Powershell script

Posted on 2016-11-03
Last Modified: 2016-11-23
Requirements of the script:

Input: Display name, alias or SMTP address of a mailbox
Output:  Can be selected by user which is one of the properties of the command "Get-Mailbox -Identity <username> |fl "

Example for Output selection by user: ExtensionCustomAttribute1
Question by:Exchange Admin
  • 3
  • 2
  • 2
  • +2
LVL 14

Expert Comment

by:Jason Crawford
ID: 41873503
I'm trying to understand what the requirements are and I'm having a hard time.  Do you need a GUI?  Should a list of all possible parameters for Get-Mailbox exist in something like a drop down menu?

Expert Comment

ID: 41873705

As a basic script you can do something like this:
$UserInputName = Read-Host "`t`tPlease enter the user displayname"
$AttributeInput = Read-Host "`t`tPlease enter the required attribute"
get-mailbox -identity $UserInputName | select $AttributeInput

Assisted Solution

Andy earned 250 total points
ID: 41873828

I've updated with the other requirements for you.
This is all using the Exchange Shell, hence the import of the AD module
Import-Module ActiveDirectory

$UserDispName = Read-Host "`t`tPlease enter the user displayname"
$AttributeInput = Read-Host "`t`tPlease enter the required attribute"
get-mailbox -identity $UserInputName | select $AttributeInput

$userAlias = Read-Host "`t`tPlease enter the user Alias"
$UserADAlias = Get-ADUser -Filter {mailNickName -eq $userAlias} | %{$_.SamAccountName}
$AttributeInput = Read-Host "`t`tPlease enter the required attribute"
get-mailbox -identity $UserADAlias | select $AttributeInput

$usermail = Read-Host "`t`tPlease enter the user e-mail address"
$UserADName = Get-ADUser -Filter {Emailaddress -eq $usermail} | %{$_.SamAccountName}
$AttributeInput = Read-Host "`t`tPlease enter the required attribute"
get-mailbox -identity $UserADName | select $AttributeInput
ID: 41874948
I read like Jason did.  Needing a GUI?   Close as I can get.

# GUI version using Input Box.  This is for the alias.

[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null
	$ClientUser = [Microsoft.VisualBasic.Interaction]::InputBox("Please enter users alias", "Enter")

	Get-Mailbox -Filter * -Identity $clientUser -ResultSize unlimited | select name, customattribute1 |Out-GridView

Open in new window

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails


Author Comment

by:Exchange Admin
ID: 41875145
Hi Todd and Jason,

Thanks for your response. The requirement is a powershell script which should meet the below criteria:

      An excel file with a set of users (which could be either email address or alias or display name) is provided to script during its run time as input.
     Script should prompt user to select the output which could be any attributes of Get-mailbox command result. And the selection of user can be one or many
    If mailbox is not present, output should be "Not found"

Please check the attachment.

LVL 14

Expert Comment

by:Jason Crawford
ID: 41875362
Just to make sure I'm on the same page.  A .csv file containing one column populated with rows of users will be provided.  The script should import the list of users, then display a full list of all parameters for the Get-Mailbox cmdlet (I'm guessing a numbered list).  The user will enter the number mapped to the parameter they want included in the output.  For each user in the .csv file the script will run Get-Mailbox and gather the pre-selected parameters.  I assume you want the output to be a ,csv file on the desktop?  Is that all right?
LVL 68

Accepted Solution

Qlemo earned 250 total points
ID: 41875505
We can use Excel - if really needed. But it is not recommended, as access is slow. Better, and probably what you really want, is the use of CSV files as Jason mentioned above. Though using a single column CSV is overkill; you usually leave out the column header and store as text file then.

Mixing CSV in/output with an interactive grid view for the attributes choice (even allowing for selecting multiple attributes):
$attr = '*'
$forcedAttr = 'DisplayName', 'UserPrincipalName', 'Database', 'WhenCreated'
Import-CSV C:\Temp\EE\Users.csv |
  % {
    $mb = Get-Mailbox $_.{Display Name} -Filter $attr
    if ($attr -eq '*')   # we need to show an attribute choice opportunity once
      $attr = $mb | Get-Member -Type Property | Select Name |
                    ? { $forcedAttr -notcontains $_.Name } | 
                    Out-GridView -OutputMode Multiple -Title "Select attributes to export"
      $attr = $forcedAttr + $attr
    $mb | select $attr  
  } |
  Export-CSV -NoType C:\Temp\EE\Results.csv

Open in new window

With some more effort we can display example values (of the first mailbox) when showing the grid, or exclude attributes which should never be exportable.

Author Comment

by:Exchange Admin
ID: 41876216
Hi Jason,

You are correct, but the output selection by user is not a number. User can enter the parameter required by them during run of script. And this parameter can be one or many.


Author Closing Comment

by:Exchange Admin
ID: 41899024
Solutions were provided

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In this previous article (, we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
This article will help you understand what HashTables are and how to use them in PowerShell.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at

706 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

15 Experts available now in Live!

Get 1:1 Help Now