Script to search for folder then create output

Hello,
I’m very novice at Powershell and get most of what I would like from browsing Google. I am close to wait i’m trying to achieve but can’t get it complete.
I want to scan AD for all PCs that are like xxxx.
scan a folder on those PCs for child folder a or b or c or d
If folder exists assign a number to each folder
Output computer name and 1 or 2 or 3 or 4.
Below is what i have so far:

$computers = Get-ADComputer -Filter "name -like 'x-ray-*'" |
	Foreach ($computer in $computers){
	if(Test-Path "\\$computer\c$\programdata\tw\a"){
	VALUE = "A"
 } elseif (Test-Path "\\$computer\c$\programdata\tw\b"){
	VALUE = "B"
 } elseif (Test-Path "\\$computer\c$\programdata\tw\c"){
	VALUE = "C"
} elseif (Test-Path "\\$computer\c$\programdata\tw\d"){
	VALUE = "D"
}}
export-csv c:\users\cn0922\desktop\81003D.csv "$computer, VALUE

Any assistance would be appreciated.
Thanks

Mike,
Welcome to the forum. :wave:t4:

Before we proceed - could you please go back, edit your question and fix the formatting of the code you posted?

Along with the code you used and got stuck with you should post any error messages if there is one.

I have no error code. I have had a bunch of mixes results trying to get where i want to be and the errors are all different. From error in my code to the output only being the first PC name and the path of the file. That was the closest I came to achieving my goal.
The term VALUE in my code i don’t know what that should be.
Thanks

If you run the code you posted it is impossible not to get an error message because your code is syntactically incorrect. :wink:

You may start with something like this:

$ValueList = 'A', 'B', 'C', 'D'
$SearchBase = 'OU=Computer,OU=Berlin,OU=Germany,OU=Europe,DC=contoso,DC=com'
$ComputerList = Get-ADComputer -SearchBase $SearchBase -Filter "name -like 'x-ray-*'"
$Result =
foreach ($ComputerName in $ComputerList.Name ) {
    foreach ($Value in $ValueList) {
        if (Test-Path -Path "\\$($ComputerName)\c$\programdata\tw\$($Value)") {
            [PSCustomObject]@{
                ComputerName = $ComputerName
                Value        = $Value
            }
        }
    }
}
$Result
$Result |
Export-Csv -Path 'c:\users\cn0922\desktop\81003D.csv' -NoTypeInformation

To reduce the stress you put on your AD it is recommended to use a searchbase when you query the AD.

Olaf, thank you very much. That worked like a charm. I hope to someday have the same understanding of powershell that you have.
I am able to work with this data. this will be very helpful.
if you would like another “challenge”, have the code output a different value.
for example, if $value A exists assign #10 and output computername and #10, or if $value B exists assign #20, etc.

Because it is not a challenge for me but for you, I leave it to you. :wink: The hint to accomplish something like this easily is “Hashtable”!! :stuck_out_tongue_winking_eye: :smiley:

1 Like

:slight_smile: i’ll work on it and let you know how i do. thanks