PowerShell FAQsThis page is dedicated to questions related to PowerShell.
Q: I am trying to set my default Powershell user profile file so that when I launch Powershell, it automatically changes the recipient scope to view the entire forest. Simply typing in this $AdminSessionADSettings.ViewEntireForest=$True just doesn't work. Though I know the profile does as I have tested it with other entries such as get-process. A: In Bin\Exchange.ps1 (the profile) fine the line which says "$global:AdminSessionADSettings.ViewEntireForest = $false" and change it to "$global:AdminSessionADSettings.ViewEntireForest = $false".
Q: How do I set a schedule using the Cmdline? For example setting the MaintenanceSchedule property on set-MailboxDatabase or Set-PublicFolderDatabase.
[PS] D:\>set-mailboxdatabase "Mailbox Database" -MaintenanceSchedule "Mon.10:00 PM-Mon.10:15 PM, Tue.11:00 PM-Tue.11:30 PM"
There are some build-in MaintenanceSchedule defined:
Never Always Daily10PM Daily11PM Daily12PM DailyAtMidnight Daily1AM Daily2AM Daily3AM Daily4AM Daily5AM DailyFrom11PMTo3AM DailyFrom1AMTo5AM DailyFrom2AMTo6AM DailyFromMidnightTo4AM EveryFourHours EveryHour EveryTwoHours FridayAtMidnight SaturdayAtMidnight SundayAtMidnight DailyFrom8AMTo5PMAtWeekDays DailyFrom9AMTo5PMAtWeekDays DailyFrom9AMTo6PMAtWeekDays DailyFrom8AMTo12PMAnd1PMTo5PMAtWeekDays DailyFrom9AMTo12PMAnd1PMTo6PMAtWeekDays
You can use them to set the MaintenanceSchedule using :
[PS] D:\>set-mailboxdatabase "Mailbox Database" -MaintenanceSchedule @([Microsoft.Exchange.Data.Schedule]::Daily10PM)
You can also specify custom scheduled times in the following way:
[PS] D:\>set-mailboxdatabase "Mailbox Database" -MaintenanceSchedule "Mon.10:00 PM-Mon.10:15 PM, Tue.11:00 PM-Tue.11:30 PM"
For gory detailed scripting or custom programming, you can invoke the type to create a schedule as well:
[PS] D:\>set-mailboxdatabase "Mailbox Database" -MaintenanceSchedule @([Microsoft.Exchange.Common.ScheduleInterval](new-object Microsoft.Exchange.Common.ScheduleInterval ("Monday", 12, 0, "Monday", 13, 0)))
The signature is:
(WeekDayAndTime startTime, WeekDayAndTime endTime) (DayOfWeek startDay, int startHour, int startMinute, DayOfWeek endDay, int endHour, int endMinute)
Q: I start powershell.exe but I get a "command not found" error everytime I run an Exchange command. What's up?
A: Exchange uses the "snapin" model for extending powershell with their cmdlets (kind of like MMC.exe and the GUI Exchange snapin). When you just type in start->run->powershell.exe, you're seeing vanilla powershell without the Exchange snapin loaded. You can get to the Exchange stuff by doing:
option 1) Start->Programs->Exchange Server->Exchange Mangement Shell. This shortcut will automatically start powershell + load our snapin
option 2) For the folks who often do start-run->powershell.exe you can simply type add-mshsnapin *exchange* and it will load the exchange snapins
option 3) For the folks who think the above step is even too much (like me) :), you can do: start-run-> exshell.msc1
Note: after beta 2 Monad will be renamed to PowerShell, so add-mshsnapin will be renamed to add-pssnapn and the extension will be exshell.psc1.
Q: Ok, I've seen commands with colons ":" and without them... which syntax is right?
A: Colons are optional except for Switch parameters such as -confirm, -verbose etc. You need to use them when specifying $false (turn a switch OFF) to switch parameters:
set-mailbox something -verbose:$false
So, you can omit them with all other parameter types except switches.
Q: How do I enable "log all powershell cmds to the local event log" feature in PowerShell v1?
A: Its a registry key under each snapin that is registered with PowerShell. Here is how you turn it on for the Exchange admin snapin
set-itemproperty HKLM:\SOFTWARE\Microsoft\PowerShell\1\PowerShellSnapIns\Microsoft.Exchange.Management.PowerShell.Admin -Name LogpipelineExecutionDetails -value 1
Q: I heard there was a virus for Powershell, should I be worried?
A: PowerShell is a scripting language, so it can be used to a variety of things, some of which may be malicious. Learning from past experience, PowerShell team has done a great job of building a secure system. Out of the box, it is very hard to run malicious scripts. PowerShell team blogged about this in detail recently, be sure to read their post for more details.
Q: How do I change the prompt in the cmdline?
A: Check out this wiki page: How to change the prompt in powershell.
Q: When I run a command in powershell, all I see is a table, where are all the properties? How do I see all the properties on some output? How do I selectively see only some properties?
A: What you see in the default output is really just a summary of the real output. You can always do <cmd> | format-list (fl for short) to see all the glorious details. Format-Table, as the name implies, outputs a table. With each command you can customize what properties you want to see. get-sendconnector | format-table Name,*auth*. That last line will show all properties that contain the word "auth" and the name. Here's a blog post that describes this further.
Q: Ack! I messed up something in the input or I ran a cmd without finishing what I was doing!
A: No worries. CTRL-C will break the current operation and return you to the prompt.
Q: I like the default confirmation on destructive operations like remove-addresslist, its nice and safe... but I need to write a script that does this a 1,000 times! How do I turn off the confirmation in my script?
A: Just pass in -confirm:$false to the command in question.
Q: How do I use server side filters to the Exchange recipient commands?
A: Here is some sample syntax: get-mailbox -filter { Name -like 'bjorn*' }. In the beta the list of properties you can filter on is limited but this limitation will be fixed in the final product.
Q: What do the '?' and '%' symbols mean in PowerShell?
A: They are aliases and stand for 'Where-Object' and 'ForEach-Object' respectively. To get a complete list of aliases type 'get-alias' at the command prompt.
Q: How do I delete all .txt files from a directory and its sub-directories?
A: Check out this wiki page: Delete all .txt files
Q: How do I see objects in other domains ?
A: The scope of a query (such as get-mailbox) is controlled by a variable called $AdminSessionADSettings. To view object in other domains, try setting $AdminSessionADSettings.ViewEntireForest=$true and then issuing this command. You can also set this in your profile if you want this to be the default value.
Q: How can I schedule a Exchange script to run periodically?
You will need to leverage a task scheduler such as Windows scheduler. Since you will not be able to run the script directly you need to run PowerShell and the Exchange snapin. So your command line will look something like this:
MSH.exe -MSHConsoleFile "C:\Program Files\Microsoft\Exchange Server\Bin\ExShell.Mcf1" -Command ". '<script.msh>'"
PowerShell.exe -PSConsoleFile "C:\Program Files\Microsoft\Exchange Server\Bin\ExShell.Msc1" -Command ". '<script.ps1>'"
|