Convert Path - PowerShell


Overview

Convert-Path is a PowerShell command that resolves and converts a path (string) to a fully qualified path (FileSystemInfo object). It ensures that the path is in a standard format and allows for easy manipulation and processing.

Syntax

Convert-Path [-Path] <string> [-Root] <string> [-SkipResolve] [-ErrorAction] <ActionPreference> [-Help] [-Verbose]

Options/Flags

  • -Path: Required. Specifies the path string to be converted. Supports wildcards.
  • -Root: Optional. Specifies the root path from which to start resolving. By default, the current directory is used.
  • -SkipResolve: Optional. Disables path resolution and returns the original path string. By default, resolution is performed.
  • -ErrorAction: Optional. Specifies the action to take if an error occurs during path conversion. Default is Stop.
  • -Help: Optional. Displays help information for the command.
  • -Verbose: Optional. Enables verbose output, showing detailed information about the conversion process.

Examples

Example 1: Convert a relative path to a fully qualified one:

Convert-Path ../Documents/Report.txt

Example 2: Resolve a path using a specific root:

Convert-Path -Path Desktop\MyFolder -Root C:\Users\John

Example 3: Skip path resolution:

Convert-Path -Path *.\*.txt -SkipResolve

Common Issues

  • Invalid Paths: Ensure that the specified path exists and is valid. If not, the conversion may fail.
  • Wildcard Expansion: If using wildcards in the path, ensure they refer to actual files or directories. Otherwise, the command may return an empty or invalid result.

Integration

Combine with other Commands:

Get-ChildItem | Convert-Path -Root C:\Temp | Move-Item -Destination C:\Backup

Use in Scripts:

$files = Get-ChildItem -Recurse 'C:\MyData'
foreach ($file in $files) {
    $newPath = Convert-Path -Path $file.FullName -Root C:\Backup
    Copy-Item -Path $file.FullName -Destination $newPath
}