Solved

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

Posted on 2016-08-04
4
29 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 84

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 84

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

697 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