PowerShell for Windows Admins

Jun 29 2017   1:50PM GMT

Just the folders

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Tags:
Powershell

Lets say you have a bunch of files in nested folders but you want just the folders not the file or drive

Our files look like this

C:\Scripts\HyperV\Setup
 C:\Scripts\HyperV\attachdisks.ps1
 C:\Scripts\HyperV\get-mountedvhdDrive.ps1
 C:\Scripts\HyperV\invoke-CIMshutdown.ps1
 C:\Scripts\HyperV\set-loopbackswitch.ps1
 C:\Scripts\HyperV\Set-NestedVirtualisation.ps1
 C:\Scripts\HyperV\set-realswitch.ps1
 C:\Scripts\HyperV\Start-AllWindowsVMs.ps1
 C:\Scripts\HyperV\Stop-AllVMs.ps1
 C:\Scripts\HyperV\Stop-Lab.ps1
 C:\Scripts\HyperV\test-HotfixIpresence.ps1
 C:\Scripts\HyperV\Setup\Copy-Updates.ps1
 C:\Scripts\HyperV\Setup\Get-LicenseStatus.ps1
 C:\Scripts\HyperV\Setup\Install-RollUp.ps1
 C:\Scripts\HyperV\Setup\New-VirtualMachine.ps1
 C:\Scripts\HyperV\Setup\SCsetup.ps1
 C:\Scripts\HyperV\Setup\Set-VMconfig1.ps1
 C:\Scripts\HyperV\Setup\Set-VMconfig2.ps1
 C:\Scripts\HyperV\Setup\setup (2).ps1
 C:\Scripts\HyperV\Setup\setup.ps1
 C:\Scripts\HyperV\Setup\sysprep.txt

You can strip out the file names using Split-Path and the –Parent parameter

PS> Get-ChildItem .\HyperV\ -Recurse | foreach {Split-Path -Path $_.Fullname -Parent }
 C:\Scripts\HyperV
 C:\Scripts\HyperV
 C:\Scripts\HyperV
 C:\Scripts\HyperV
 C:\Scripts\HyperV
 C:\Scripts\HyperV
 C:\Scripts\HyperV
 C:\Scripts\HyperV
 C:\Scripts\HyperV
 C:\Scripts\HyperV
 C:\Scripts\HyperV
 C:\Scripts\HyperV\Setup
 C:\Scripts\HyperV\Setup
 C:\Scripts\HyperV\Setup
 C:\Scripts\HyperV\Setup
 C:\Scripts\HyperV\Setup
 C:\Scripts\HyperV\Setup
 C:\Scripts\HyperV\Setup
 C:\Scripts\HyperV\Setup
 C:\Scripts\HyperV\Setup
 C:\Scripts\HyperV\Setup

Split-Path also has a –NoQualifier parameter that strips of the drive – unfortunately its in a different parameter set to –Parent BUT you can use a pipeline

PS> Get-ChildItem .\HyperV\ -Recurse | foreach {Split-Path -Path $_.Fullname -Parent | Split-Path -NoQualifier }
 \Scripts\HyperV
 \Scripts\HyperV
 \Scripts\HyperV
 \Scripts\HyperV
 \Scripts\HyperV
 \Scripts\HyperV
 \Scripts\HyperV
 \Scripts\HyperV
 \Scripts\HyperV
 \Scripts\HyperV
 \Scripts\HyperV
 \Scripts\HyperV\Setup
 \Scripts\HyperV\Setup
 \Scripts\HyperV\Setup
 \Scripts\HyperV\Setup
 \Scripts\HyperV\Setup
 \Scripts\HyperV\Setup
 \Scripts\HyperV\Setup
 \Scripts\HyperV\Setup
 \Scripts\HyperV\Setup
 \Scripts\HyperV\Setup

If you just want the folders and don’t care about the files try

PS> Get-ChildItem -Path .\ -Recurse -Directory | 
 where FullName -Like "*HyperV*" | 
foreach {Split-Path -Path $_.Fullname -Parent | Split-Path -NoQualifier }
 \Scripts
 \Scripts\HyperV

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

Share this item with your network: