0

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?

7
  • So, to rephrase, you're looking to compare the group names from Get-ADGroup, to the folder names returned by Get-ChildItem D:\Test? Then, create the folders based on which one's missing? Commented May 4, 2022 at 12:56
  • the idea is that for a group (e.g. Test) if the extensionAttribute1 is set to folder, a new folder with the group names is created on the server drive D:\ if no folder exists yet Commented May 5, 2022 at 13:10
  • That's still confusing, probably why noone has answered yet lol. You want a single folder for the group names, or separate folders per group that has the value of Folder in extensionAttribute1? Commented May 5, 2022 at 13:30
  • I would like to create a new directory for each group where the atriubet is set that has the group name. and of course the script should not try to create a folder if it already exists Commented May 6, 2022 at 14:29
  • Can we see your attempt at this? Commented May 6, 2022 at 22:59

1 Answer 1

0

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 
            }
        }

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.