Format‑Path - dsccommunity/DscResource.Common GitHub Wiki

SYNOPSIS

Normalizes a file system path.

SYNTAX

Format-Path [-Path] <String> [-EnsureDriveLetterRoot] [-NoTrailingDirectorySeparator]
 [<CommonParameters>]

DESCRIPTION

Normalizes a file system path to ensure proper formatting based on specified preferences. The function can add a backslash to drive letter paths and remove trailing backslashes from directory paths based on the parameters provided. It will recognize Windows drive paths and UNC paths and make sure they are formatted with backslashes, regardless of the operating system. Relative paths or absolute paths (that start with slash or backslash) will always be converted to use the system's directory separator.

When no formatting parameters are specified, the function only normalizes directory separators (using backslashes for Windows paths and the system's directory separator for other paths) while preserving any trailing separators.

EXAMPLES

EXAMPLE 1

Format-Path -Path 'C:/MyFolder/'

Returns 'C:\MyFolder'. Only normalizes directory separators when no formatting parameters are specified.

EXAMPLE 2

Format-Path -Path 'C:' -EnsureDriveLetterRoot

Returns 'C:'

EXAMPLE 3

Format-Path -Path 'C:\MyFolder\' -NoTrailingDirectorySeparator

Returns 'C:\MyFolder'

EXAMPLE 4

Format-Path -Path 'C:\MyFolder\' -EnsureDriveLetterRoot -NoTrailingDirectorySeparator

Returns 'C:\MyFolder'

EXAMPLE 5

Format-Path -Path 'C:' -EnsureDriveLetterRoot -NoTrailingDirectorySeparator

Returns 'C:'. The EnsureDriveLetterRoot parameter takes precedence over NoTrailingDirectorySeparator for drive letter paths.

EXAMPLE 6

Format-Path -Path 'MyFolder/SubFolder\'

Returns 'MyFolder\SubFolder' on Windows or 'MyFolder/SubFolder/' on Linux/macOS. Relative paths are normalized to use the system's directory separator.

EXAMPLE 7

Format-Path -Path '/var/log/' -NoTrailingDirectorySeparator

Returns '/var/log' on Linux/macOS or '\var\log' on Windows. Unix-style absolute paths are normalized to use the system's directory separator.

PARAMETERS

-EnsureDriveLetterRoot

When specified, adds a trailing backslash to paths that consist of only a drive letter (e.g., 'C:' becomes 'C:'). This parameter takes precedence over NoTrailingDirectorySeparator for drive letter paths.

Type: SwitchParameter
Parameter Sets: (All)
Aliases:

Required: False
Position: Named
Default value: False
Accept pipeline input: False
Accept wildcard characters: False

-NoTrailingDirectorySeparator

When specified, removes any trailing directory separator (backslash) from paths, including drive letters.

Type: SwitchParameter
Parameter Sets: (All)
Aliases:

Required: False
Position: Named
Default value: False
Accept pipeline input: False
Accept wildcard characters: False

-Path

The file system path to normalize.

Type: String
Parameter Sets: (All)
Aliases:

Required: True
Position: 1
Default value: None
Accept pipeline input: False
Accept wildcard characters: False

CommonParameters

This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters.

INPUTS

OUTPUTS

System.String

NOTES

RELATED LINKS

⚠️ **GitHub.com Fallback** ⚠️