Fairly new to PowerShell and am looking to figure out how to find a name of a directory that matches multiple criteria from an array list.
The below code is what has been developed so far:
$InputAddress = Read-Host -Prompt 'Enter in a job address'
# Remove any non alphanumeric characters and white spaces
$InputAddress = ($InputAddress -replace '[^a-zA-Z\d\s]')
# Remove any double spaces and split to separate into an array
$InputAddress = [System.Collections.ArrayList]($InputAddress -replace ' ',' ').split(" ")
while ($InputAddress -contains "USA")
{
$InputAddress.Remove("USA")
}
$srcRoot = (Get-ChildItem -Path 'C:\Users\' -Filter "Box" -Recurse -Directory -Depth 1 -ErrorAction:Ignore).Fullname+'\'
$JobRoot = (Get-ChildItem -Path $srcRoot -Filter "*Backup*" -Recurse -Directory -Depth 0).Fullname
$JobFolder = (Get-ChildItem -Path $JobRoot -Filter "$InputAddress" -Recurse -Directory -Depth 0)
Write-Output $JobFolder
The address that is inputted from Read-Host
is "1234 street - cityname, CA USA", which when it goes through the script creates an array list of:
1234
street
cityname
CA
The intended folder that would need to be found is named "1234 street, cityname, CA - ABC Company"
Some background info is PowerShell 5.1 is being used, Windows 10 OS.
Could someone help me understand how to include all criteria when performing the Get-ChildItem
? My current best guess is that either wildcards *
are needed, or -and
type to include all items.
Edit: Some more background regarding the folder structure of all the folders that are named as addresses-is that all folders start with the number, then the street name, then sometimes the state/province. If the street number narrows down the amount of folders, and then the street name narrows it down to one folder, this would be ideal. Basically each ordered element could narrow the search further until only one folder is returned.