Just the folders

Lets say you have a bunch of files in nested folders but  you just want 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
Advertisements
This entry was posted in Powershell. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s