Skip to content

Commit f8242e2

Browse files
authored
Format-Path add ExpandEnvironmentVariable Parameter (#152)
1 parent 138446c commit f8242e2

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-4
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- `Format-Path`
13+
- Added parameter `ExpandEnvironmentVariable` fixes [#147](https://github.com/dsccommunity/DscResource.Common/issues/147).
14+
1015
## [0.22.0] - 2025-04-25
1116

1217
### Removed

source/Public/Format-Path.ps1

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@
2727
When specified, removes any trailing directory separator (backslash) from
2828
paths, including drive letters.
2929
30+
.PARAMETER ExpandEnvironmentVariable
31+
Replaces the name of each environment variable embedded in the specified string
32+
with the string equivalent of the value of the variable.
33+
Each environment variable must be quoted with the percent sign character (%).
34+
3035
.EXAMPLE
3136
Format-Path -Path 'C:/MyFolder/'
3237
@@ -65,6 +70,11 @@
6570
6671
Returns '/var/log' on Linux/macOS or '\var\log' on Windows.
6772
Unix-style absolute paths are normalized to use the system's directory separator.
73+
74+
.EXAMPLE
75+
Format-Path -Path '%WinDir%\SubFolder' -ExpandEnvironmentVariable
76+
77+
Returns the path with the environment variable expanded, e.g., 'C:\Windows\SubFolder'
6878
#>
6979
function Format-Path
7080
{
@@ -82,17 +92,29 @@ function Format-Path
8292

8393
[Parameter()]
8494
[System.Management.Automation.SwitchParameter]
85-
$NoTrailingDirectorySeparator
95+
$NoTrailingDirectorySeparator,
96+
97+
[Parameter()]
98+
[System.Management.Automation.SwitchParameter]
99+
$ExpandEnvironmentVariable
86100
)
87101

88-
if ($Path -match '^(?:[a-zA-Z]:|\\\\)')
102+
# Use local variable so it always exists.
103+
$normalizedPath = $Path
104+
105+
if ($ExpandEnvironmentVariable)
106+
{
107+
$normalizedPath = [System.Environment]::ExpandEnvironmentVariables($normalizedPath)
108+
}
109+
110+
if ($normalizedPath -match '^(?:[a-zA-Z]:|\\\\)')
89111
{
90112
# Path starts with a Windows drive letter, normalize to backslashes.
91-
$normalizedPath = $Path -replace '/', '\'
113+
$normalizedPath = $normalizedPath -replace '/', '\'
92114
}
93115
else
94116
{
95-
$normalizedPath = $Path -replace '[\\|/]', [System.IO.Path]::DirectorySeparatorChar
117+
$normalizedPath = $normalizedPath -replace '[\\|/]', [System.IO.Path]::DirectorySeparatorChar
96118
}
97119

98120
# Remove trailing backslash if parameter is specified and path is not just a drive root.

tests/Unit/Public/Format-Path.Tests.ps1

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,4 +165,21 @@ Describe 'Format-Path' {
165165
}
166166
}
167167
}
168+
169+
Context 'When using ExpandEnvironmentVariable parameter' {
170+
It 'Should expand environment variables in the path' {
171+
$env:TestPath = 'C:\TestFolder'
172+
Format-Path -Path '%TestPath%' -ExpandEnvironmentVariable | Should -Be 'C:\TestFolder'
173+
}
174+
175+
It 'Should handle paths with multiple environment variables' -Skip:($IsLinux -or $IsMacOS) {
176+
$env:BasePath = 'C:\Base'
177+
$env:SubPath = 'SubFolder'
178+
Format-Path -Path '%BasePath%\%SubPath%' -ExpandEnvironmentVariable | Should -Be 'C:\Base\SubFolder'
179+
}
180+
181+
It 'Should not modify paths without environment variables' {
182+
Format-Path -Path 'C:\NoEnvVar' -ExpandEnvironmentVariable | Should -Be 'C:\NoEnvVar'
183+
}
184+
}
168185
}

0 commit comments

Comments
 (0)