RayofCommand
RayofCommand

Reputation: 4244

Report folders which have a specific subfolder

I am building a script which I use to deploy files to multiple specific folders. The destination folders are collected using this part.

$destinations = Get-ChildItem "C:\this\is\*\my\path\" 

So my script replaces only if the folder has the subfolders "\my\path\"

If I now check my variable it will return the fullpathes but I only need the folder name. I tried using select -path to show at least only the path but it returned as well the length, mode etc.

my goal is to return only values like this:

folder 1
folder 2
folder 3

I am using powershell 3.0

Upvotes: 0

Views: 245

Answers (1)

Matt
Matt

Reputation: 46710

So if we are checking for folders that have the child structure folder1\folder2 where the parent folder is in C:\Temp then we would do something like this:

$destinations = (Get-Item "C:\Temp\*\folder1\folder2").Parent.Parent.Name

Get-Item "C:\Temp\*\folder1\folder2" would just return System.IO.DirectoryInfo objects for folder2. We take those objects and find their grandparent folders and just return their names only.

Upvotes: 1

Related Questions