Solved

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

Posted on 2016-08-04
4
24 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Set OWA language and time zone in Exchange for individuals, all users or per database.
A brief introduction to what I consider to be the best editor for PowerShell.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

920 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

16 Experts available now in Live!

Get 1:1 Help Now