Solved

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

Posted on 2016-08-04
4
19 Views
Last Modified: 2016-08-04
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
Comment
Question by:the_omnific
  • 2
  • 2
4 Comments
 
LVL 83

Accepted Solution

by:
oBdA earned 500 total points
ID: 41741898
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
 
LVL 1

Author Comment

by:the_omnific
ID: 41742197
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
 
LVL 83

Expert Comment

by:oBdA
ID: 41742208
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
 
LVL 1

Author Closing Comment

by:the_omnific
ID: 41742223
Excellent.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

The article will show you how you can maintain a simple logfile of all Startup and Shutdown events on Windows servers and desktops with PowerShell. The script can be easily adapted into doing more like gracefully silencing/updating your monitoring s…
A procedure for exporting installed hotfix details of remote computers using powershell
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now