Time to learn: ~ 3 minutes
Phrases: 571
Replace: Studying from my errors aka Failing Up
Replace: Reliably knowledgeable that `-AdditionalChildPath` was added after 5.1
Be part of Me For a Second
There’s a large number of scripts out within the wild with a series of Be part of-Path
instructions. Initially, after I wished to create a path safely, a Be part of-Path
cmdlets chain was additionally my go-to. Nevertheless, after studying up on the documentation, I realised one other means: I solely want a singular occasion of the Be part of-Path
command.
Goal Location
My PowerShell console is open in my dwelling
folder, and I’ve a take a look at file: /dwelling/soneill/PowerShell/pester-5-groupings/00-run-tests.ps1
.
If I wished to create a variable that goes to the situation of that file, one of many protected methods of doing that’s to make use of Be part of-Path
.
Lengthy Type
I imply, I might create the variable myself by concatenating strings, however then I’d must take the trail separator into consideration relying if I’m on Home windows or not.
Apparently not…
$var = ".PowerShellpester-5-groupings 0-run-tests.ps1"
[PSCustomObject] @{
Kind="Lengthy Type"
Separator="Guide entry: "
Variable = $var
Path = attempt {Get-ChildItem -Path $var -ErrorAction Cease} catch {'Error!'}
}
I assumed this wouldn’t work however, when operating the code samples, it seems that PowerShell doesn’t thoughts me utilizing a forward-slash (/
) or a back-slash (); it’ll care for the correct separator for me.
UPDATE: This manner works superb from a file however run the script from a PowerShell terminal and it’s a no-go.
UPDATED UPDATE: Thanks for Cory Knox (twitter) and Steven Judd (twitter) for declaring that this fails as a result of it’s utilizing /bin/ls
as an alternative of the Get-ChildItem
alias:
Guide Creation
A extra specific, cross-platform technique could be to make use of the [IO.Path]::DirectorySeparatorChar
.
$sep = [IO.Path]::DirectorySeparatorChar
$var = ".${sep}PowerShell${sep}pester-5-groupings${sep}00-run-tests.ps1"
[PSCustomObject] @{
Kind="Guide Creation"
Separator = "[IO.Path]::DirectorySepartorChar: $sep"
Variable = $var
Path = attempt {Get-ChildItem -Path $var -ErrorAction Cease} catch {'Error!'}
}
This technique works superb however creating the trail can get very lengthy if I don’t use a variable. Even utilizing a variable, I’ve to wrap the identify in curly braces due to the string growth technique I used. That’s not one thing that I’d count on somebody selecting up PowerShell for the primary time to know.
-f Strings
In case you’re questioning, one other string growth technique right here could be to make use of -f
strings.
$sep = [IO.Path]::DirectorySeparatorChar
$varf=".{0}PowerShell{0}pester-5-groupings{0}00-run-tests.ps1" -f $sep
[PSCustomObject] @{
Kind="F String"
Separator = "[IO.Path]::DirectorySepartorChar: $sep"
Variable = $varf
Path = attempt {Get-ChildItem -Path $varf -ErrorAction Cease} catch {'Error!'}
}
Many Be part of-Path Instructions
Higher but could be if I didn’t must account for the separator in any respect. Right here’s the place the a number of Be part of-Path
cmdlets come into play.
$var2 = Be part of-Path -Path . -ChildPath PowerShell | Be part of-Path -ChildPath pester-5-groupings | Be part of-Path -ChildPath 00-run-tests.ps1
[PSCustomObject] @{
Kind="Many be part of paths"
Separator="Taken care of: Be part of-Path"
Variable = $var2
Path = attempt {Get-ChildItem -Path $var2 -ErrorAction Cease} catch {'Error!'}
}
A number of Be part of-Path
instructions work superb. No actual subject with folks utilizing this manner, however there may be one other!
Solely One Be part of-Path Wanted
Be part of-Path
has a parameter known as -AdditionalChildPath
that takes the remaining arguments from the command line and makes use of them in a lot the identical means as a Be part of-Path
command chain would.
$var3 = Be part of-Path -Path . -ChildPath PowerShell -AdditionalChildPath 'pester-5-groupings', '00-run-tests.ps1'
[PSCustomObject] @{
Kind="AdditionalChildPaths"
Separator="Taken care of: Be part of-Path"
Variable = $var3
Path = attempt {Get-ChildItem -Path $var3 -ErrorAction Cease} catch {'Error!'}
}
Extra Output than Put Out
So there you go—multiple option to be part of a path. Use whichever ones give you the results you want. It’s good to know your choices, although.