Jcircuit
Jcircuit

Reputation: 63

How to generate a selection list from the results of a previous command and set the answer to a variable

I'm attempting to create an onboarding script to automate the AD account creation process. Within the script I would like to use the command Get-ADOrganizationalUnit -Filter 'Name -like "\*"' to get a list of all OUs, then generate a selection list within the PowerShell script to go with the names of each OU. Once the name is selected I would like to set a variable equal to that OUs distinguishedname. Any suggestions on the best way to accomplish this?

Upvotes: 1

Views: 1755

Answers (1)

Santiago Squarzon
Santiago Squarzon

Reputation: 60528

You can use Out-GridView with the -PassThru switch to allow the, not sure if "the best way" but is an easy and friendly alternative.

$ous = Get-ADOrganizationalUnit -Filter 'Name -like "\*"'
$choice = $ous | Select-Object Name, DistinguishedName | Out-GridView -PassThru
if($choice) { # if the user selected an item from the DGV and pressed `OK`
    $choice.DistinguishedName
}
else { # user clicked `Cancel` or closed the DGV
    exit
}

Upvotes: 1

Related Questions