Home > Mobile >  Powershell: Multiple subfolders named the same. Want all tied to Path variable
Powershell: Multiple subfolders named the same. Want all tied to Path variable

Time:05-21

Is there a simple way of taking multiple subfolders under the same root and assigning it to a single variable?

My folder structure looks like the following

C:\Unsigned Items\City A\Jeff\Signed

C:\Unsigned Items\City B\Erik\Signed

C:\Unsigned Items\City C\Dave\Signed

I want to assign all of the subfolders named "Signed" to a single variable, such as $srcRoot

I started by just adding each path like the following. It works, but seems inefficient as I have about 50 "cities".

$srcRoot = "C:\Unsigned Items\City A\Jeff\Signed","C:\Unsigned Items\City B\Erik\Signed" ect

CodePudding user response:

So I gather you want an aray of directory fullnames from a source folder where two of the subfolder paths have different named, but all contain a folder called Signed.

Just use Get-ChildItem starting with the path all folders have in common (C:\Unsigned Items) and for the subfolders with different names simply use the wildcard asteriks.
Then take the FullName property from the resulting DirectoryInfo objects:

$srcRoot = (Get-ChildItem -Path 'C:\Unsigned Items\*\*\Signed' -Directory).FullName
  • Related