29 November 2016

PowerShell tips

This cmdlet will return all exchange specific cmdlets

Get-command -name get-service –synopsis

Get-excommand

Get-Help is another one which comes handy. Say, while you were playing with Exchange management shell, you saw a new cmdlet and don’t know what that means.

For example, you don’t know what Get-Message means. To know more, type

Get-Help Get-Message

Get-Message is a cmdlet to retrieve messages from the message queue in Exchange 2007 and 2010

An example is,

Get-Message -Filter {FromAddress -like “*@ratishnair.com”}

This cmdlet will retrieve all messages from the domain “ratishnair.com”
The tilde character (~) represents the shortcut to root directory. For example,

Dir ~

Use it as a shortcut:

Cp FileName “~\My Documents”

We see the $_ sign in a lot of cmdlets. This is known as a Special Variable.

The $_ represents the objects being passed from one cmdlet to another cmdlet in the pipeline. The $_ variable is automatically initiated by the shell and is bound to the current pipeline object. You can access the properties of the object assigned to the $_ variable as you would any other object.

The following example shows how you can view the Name property of each mailbox object
that is passed through the pipeline:

Get-Mailbox | ForEach { $_.Name }

If you need another example, this one lists all running services on an Exchange server

Get-Service | where {$_.Status -eq “Running”}

Lets take a look at another one. This one returns the name of the smallest database:

(Get-MailboxDatabase | foreach { get-childitem $_.edbFilePath | select-object name,length} | sort -property length )[0]

-FILTER and –LIKE commands are often used together. This will filter the information in Double quotes and look for data matching info in Single quotes

Again, easy when explained with an example:

Get-Mailbox –RESULTSIZE UNLIMITED -FILTER “Displayname –Like ‘Nair, Ratish'”

So, this cmdlet is going to get mailbox information where the result will be unlimited and will filter information to ensure only the one with Displayname ‘Nair, Ratish’ is returned.

You can access the event log from the Exchange Management Shell. To retrieve the whole event log, run:

Get-EventLog Application | Format-List

To retrieve all Exchange-related events, run:

Get-EventLog Application | Where { $_.Source -Ilike “*Exchange*” }

Any cmdlet that accepts a size value lets you specify whether the integer value is in kilobytes (KB), megabytes (MB), gigabytes (GB), or terabytes (TB). For example:

Set-Mailbox “Meera Nair” -ProhibitSendQuota 200MB

You can import CSV files and treat them as objects by using the Import-Csv cmdlet. Each row in a CSV file becomes an element in an array, and each column becomes a property. You can assign the CSV file to a variable or you can pipe its contents directly to another cmdlet. In the following example, there are three columns in the CSV file, Name, Alias and EmailAddress, with several rows that the For Each cmdlet will cycle through. The data in each row is used to create a new mail contact.

Import-Csv | ForEach { New-MailContact -Name $_.Name -Alias $_.Alias -ExternalEmailAddress $_.EmailAddress -OrganizationalUnit Users }

The Exchange Management Shell can log all the Exchange-related commands that modify objects in some way. Exchange-related command activity is logged to the PowerShell event log. To enable Exchange-related command logging, run the following command:

Set-ItemProperty HKLM:\SOFTWARE\Microsoft\PowerShell\1\PowerShellSnapIns\Microsoft.Exchange.Management.PowerShell.Admin -Name LogpipelineExecutionDetails -value 1

This one is one of my favourites – Set-Alias

Say you want to have an Alias for Get-StorageGroup, just type

Set-Alias GetSg Get-StorageGroup
From now, Get-StorageGroup cmdlet will have an alias Get-Sg

For all the current aliases, type:

Get-Alias
This one is not exactly a powershell cmdlet tip. This works in command prompt too. CTRL+C will hard-break command in the Exchange Management Shell. If a command is taking too long to run or you want to cancel an operation quickly, press CTRL+C to stop execution.

When some or a group of users complain that they cannot access their mailbox, check the mounted status of all mailbox databases? Type:

Get-MailboxDatabase -Status | Format-Table Name, Server, Mounted

This one checks the permissions an Active Directory user account has on a specific mailbox? Use:

Get-Mailbox <Mailbox to Check> | Get-MailboxPermission -User <Active Directory User>

This one returns the list of all devices synchronized with a user’s mailbox:

Get-ActiveSyncDeviceStatistics
A variety of information is returned including device name, operating system, and last sync time.

This one returns the list of the backup status of all mailbox databases in your organization? Type:

Get-MailboxDatabase -Status | Format-Table Name, Server, *Backup*
How about just the mailboxes on a specific server? Type:

Get-MailboxDatabase -Server <Server Name> -Status | Format-Table Name, *Backup*

This one gets a list of all users who are Unified Messaging-enabled type, use:

Get-UmMailbox | ForEach { If($_.UmEnabled -Eq $True){$_.Name}}

This one will help you control the properties of e-mail messages sent to a specific domain using the RemoteDomain cmdlet.

Create a new remote domain by using the New-RemoteDomain cmdlet. Type:

New-RemoteDomain -Name “ratishnair.com Configuration” -DomainName ratishnair.com
Then modify the properties that you want for this remote domain by using the Set-RemoteDomain cmdlet:

Set-RemoteDomain ” ratishnair.com Configuration” -AutoReplyEnabled $True -AutoForwardEnabled $True

Control which features are available to Outlook Web Access users by using the Set-OwaVirtualDirectory cmdlet. Type:

Set-OwaVirtualDirectory “OWA (Default Web Site)” -ContactsEnabled $True -ChangePasswordEnabled $True

This one will disable Outlook web access for the user sunder@msexchangeguru.com

Set-CASMailbox sunder@msexchangeguru.com-OWAEnabled:$False

Move-Mailbox of all users from server EXCH1 to server EXCH2 as follows:

Get-Mailbox -Server EXCH1 | Move-Mailbox -TargetDatabase EXCH2

The .count parameter is a good one. This helps to count the output (number of mailboxes) in a cmdlet like the one shown below:

<Get-Mailbox –Server EXCH1 –Resultsize unlimited>.count

#####################

UPDATE from Jeffery Hicks (Windows PowerShell MVP)

Good list, although I might suggest a few revisions to make them even more PowerShell like and take advantage of the pipeline.

This command (point 4):

Get-Mailbox | ForEach { $_.Name }

works and generates a nice list which is great if you wanted to save it to a text file:

Get-Mailbox | ForEach { $_.Name } | out-file Mailboxes.txt.

But to get just that property all you need is

Select-Object: Get-MailBox | Select Name

I would also suggest that this (Point 16):

Get-UmMailbox | ForEach { If($_.UmEnabled -Eq $True){$_.Name}} is more complicated than it needs to be. This is more efficient.

Get-Mailbox | where {$_.UMEnabled} | Select Name

No comments:

Post a Comment