Learn What IT Pros Need to Know About Windows 11 - August 26th at 1 PM ET! Learn What IT Pros Need to Know About Windows 11 - August 26th at 1 PM ET!
Exchange Server

Optimizing PowerShell in Exchange 2007

PowerShell introduces a substantial amount of flexibility and possibilities. With Microsoft introducing PowerShell in more products, we have the ability to perform tasks amongst several different resources, all from within a PowerShell window. Given the potential, it’s easy to spend a fair amount of time in PowerShell. It’s time now to make the environment a little friendlier. With these few changes, we’ll do just that.

Tweak the shortcut

First, we’ll tweak the shortcut that launches the Exchange Management Shell. Click on Start > All Programs > Microsoft Exchange Server 2007. Right click on Exchange Management Shell and click Properties. On the Shortcut tab, set the Start In field to be your Scripts folder as seen in Figure 1 below.

Sponsored Content

Read the Best Personal and Business Tech without Ads

Staying updated on what is happening in the technology sector is important to your career and your personal life but ads can make reading news, distracting. With Thurrott Premium, you can enjoy the best coverage in tech without the annoying ads.

Figure 1 Exchange Management Shell Properties Shortcut tab

By default, this is c:\Program Files\Microsoft\Exchange Server\Scripts. That way, you’ll get tab completion for .ps1 files in this folder, and won’t have to go hunting for them. If your environment uses a single repository for all PowerShell scripts, adjust the path accordingly.

Next, click on the Options tab. Set the Command History Buffer Size to 999 to allow a much bigger buffer. This gives us the ability to look further back into what we’ve done. Next, check the box that says Discard Old Duplicates. This way, if you run the same command several times, it only shows up in the buffer once. After all, it doesn’t make sense to show more than that, right?

Check the two boxes marked Quick Edit Mode and Insert Mode as seen in Figure 2 below. Quick Edit allows us to highlight and copy information in the PowerShell window, and Insert Mode allows us to paste information into the window.

Figure 2 Exchange Management Shell Properties Option tab

Click on the Layout tab. Set the width to 120 for both the Screen Buffer Size and the Window Size, and the Height for the Window Size to 50, as seen in Figure 3 below. This gives us a bigger window to work in, and results in less word wrapping. Note: If you use a lower resolution, you may need to adjust these values accordingly.

Figure 3 Exchange Management Shell Properties Layout tab

Now that we’ve adjusted the shortcut in the Start menu, copying that shortcut to the desktop and/or Quick Launch bar gives us a much friendlier PowerShell environment.

Speed up loading PowerShell

When PowerShell, and the Exchange Management Shell, which runs on top of PowerShell, are started, some code compilation takes place. This Just In Time compilation causes the PowerShell startup to take longer. Fortunately, we can work around this by doing a onetime compilation of the managed code to a native code. This negates the need to perform the compilation at runtime. This results in a shorter startup of PowerShell. Here’s how to perform this one time task.Copy and paste the following into Notepad:

Set-Alias ngen @(
 $ngen_path = Join-Path ${env:\windir} "Microsoft.NET\Framework"
 if(${env:PROCESSOR_ARCHITECTURE} -eq "AMD64"){
 $ngen_path = Join-Path ${env:\windir} "Microsoft.NET\Framework64"
 }
 dir $ngen_path ngen.exe -recurse | where {$_.length -gt 0} | sort -descending lastwritetime
)[0].fullName
[appdomain]::currentdomain.getassemblies() | %{ngen /nologo $_.location}

Save this as Update-Gac.ps1 in the \scripts folder for Exchange, which, as mentioned above, is by default

c:\Program Files\Microsoft\Exchange Server\scripts

Now, we’ll run the script. Click Start > All Programs > Windows PowerShell 1.0 > Windows PowerShell, (NOT Exchange Management Shell), and navigate to the scripts folder. Type Update-Gac.ps1 and press Enter. Or, test the tab completion by the earlier tweaks and type Update and hit the tab key until you see the right script, then press enter. The script will go through and make the appropriate changes to the files, as seen in Figure 4 below.

When it’s finished, close the Windows PowerShell window, and open either Windows PowerShell or the Exchange Management Shell, and you should notice that it does open and get to a prompt quicker than before. While it’s still not lightning fast, it is quicker. If you look at the code, you’ll notice it checks to see if it’s running on a 64 bit or 32 bit machine, and updates the correct files. So, the script is perfect for both 64 bit production boxes and lab servers, as well as 32 bit management workstations. We can thank Jeffery Snover of the Microsoft PowerShell team for this tidbit, and their contributor “Justin” for the 32bit/64bit flexibility.

With these changes, it should be more fun playing with PowerShell. In future articles, I’ll touch on performing more administrative tasks from within PowerShell.

Got a question? Post it on our Exchange Server Forums!

Related Topics:

Register for Advanced Microsoft 365 Day!

GET-IT: Advanced Microsoft 365 1-Day Virtual Conference - Live August 24th!

Join us on Tuesday, August 24th and hear from Microsoft MVPs and industry experts about how to take advantage of Microsoft 365 at a technical level and dive deep into the features and functionality that will make your environment more secure and compliant.

RSVP Now

Sponsored By