Requires PowerShell v3 or higher, you can check using $PSVersionTable
.*
$path = (Get-ChildItem . -Recurse -Directory | Where-Object { $_.Name -eq "Folder" }).FullName
This will give you all the directories named Folder
in current directory and its subdirectories recursively.
The query, however, can return multiple results so if you want to choose the first one use [0]
. Also, to cover the case when the query returns no results, enforce returned object to be an array using @( ... )
and check if it exists:
$f = @(Get-ChildItem . -Recurse -Directory | Where-Object { $_.Name -eq "Folder" })[0].FullName
if ( $f ) {
cd $f
}
else {
# Handle the error
}
cd
is alias to Set-Location
so you can use whichever you prefer. Important thing to remember is to use FullName
property as it contains full path to the folder.
* For PowerShell versions lower than v3, use | Where-Object {$_.PSIsContainer -eq $true}
instead of -Directory