Avatar of Carl Billington
Carl Billington
Flag for Australia asked on

Powershell: Input variable prompting a user to select an Organisational Unit from a list

I am writing a powershell script and one of the input variables that I would like to include is for the user to select an OU from a list.
 
Something like;

$OU = Read-Host (but selecting an OU from a list).
 
I hope this makes sense.
 
Thanks for your help.
Powershell

Avatar of undefined
Last Comment
Carl Billington

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
oBdA

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Carl Billington

ASKER
This is great, but is there any way of the script automatically listing all our OU's without listing them all in the section?
oBdA

Sure; start with this:
$OUs = Get-ADOrganizationalUnit -Filter * -Properties Description | Select-Object -Property Name, Description, DistinguishedName
$OU = $OUs | Out-GridView -Title 'Please select an OU and click OK' -OutputMode Single
If (-not $OU) {Exit 1}
"You've selected the following OU: '$($OU.DistinguishedName)'" | Write-Host

Open in new window

In the first line, you can of course filter further, for example with the -SearchBase or -Filter argument of Get-ADOrganizationalUnit, or by adding a Where-Clause.
Carl Billington

ASKER
Excellent.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23