'Powershell script to create folder for groups with extensionAttribute1 set

Hi I want to write a powershell scrip that creates a folder for groups in a specific OU where I have set the extensionAttribute1.

Get-ADGroup -filter * -SearchBase "OU=Groups,OU=Test,DC=test,DC=Domain,DC=en" -properties * | where {$_.extensionAttribute1}| select-object samaccountname, extensionAttribute1

so I get the list of groups with extensionAttribute1 and with

Get-ChildItem D:\Test

I get the list of already created folders. What is the best way to compare these two lists and create one for the groups for which no folder is created yet?



Solution 1:[1]

Given you're scenario, you don't have to use Get-ChildItem if you just want to see if the folder already exists. Then, you would only need Test-Path and use a loop to test for each folder.

Get-ADGroup -filter * -SearchBase "OU=Groups,OU=Test,DC=test,DC=Domain,DC=en" -properties "extensionAttribute1" | 
    Where-Object -FilterScript { $_.extensionAttribute1 -eq "Folder" } | 
    ForEach-Object `
        -Begin { $path = "D:\Test\"  } `
        -Process {
            if (-not (Test-Path -LiteralPath ($path + $_.Name))) {
                New-Item -Path $path -Name $_.Name -ItemType "Directory" | Out-Null 
            }
        }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1