Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 45
  • Last Modified:

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.
0
the_omnific
Asked:
the_omnific
  • 2
  • 2
1 Solution
 
oBdACommented:
Assuming PS 3.0 or later, you can use Out-GridView as a make-shift dialog.
$OUSelection = @(
	'OU 1'
	'OU 2'
)
$OU = $OUSelection | Out-GridView -Title 'Please select an OU and click OK' -OutputMode Single
"You've selected the following OU: '$($OU)'" | Write-Host

Open in new window

1
 
the_omnificAuthor Commented:
This is great, but is there any way of the script automatically listing all our OU's without listing them all in the section?
0
 
oBdACommented:
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.
0
 
the_omnificAuthor Commented:
Excellent.
0

Featured Post

Evaluating UTMs? Here's what you need to know!

Evaluating a UTM appliance and vendor can prove to be an overwhelming exercise.  How can you make sure that you're getting the security that your organization needs without breaking the bank? Check out our UTM Buyer's Guide for more information on what you should be looking for!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now