Sunday, March 11, 2012

PowerShell: Profile Settings

Last week, I found a cool link (listed in references) that showed me how to configure a PowerShell profile. Basically, it’s a PowerShell script that runs every time you start an instance of PowerShell. I like the idea of not having to type in the same forest command each time we started up the EMS (Exchange Management Shell); however, when I tried configure the forest command in profile script and clicked the EMS link it would error out since it ran the profile script first then the RemoteExchange.ps1 script used in the EMS shortcut. I got around this by configuring my PS profile to check for the existence of Exchange script and then running it like the EMS shortcut would. Then I added the forest command and whammo I have a PS console ready to go to work.

Here is the code from my Microsoft.PowerShell_profile.ps1 file.

#Dean's PowerShell Console Settings
#Place Code in C:\Users\UserID\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
#Create Required File Using: New-item -path $profile -type file -force

#Change Console Colors to Black and Green
$host.UI.RawUI.BackgroundColor = "Black";
$host.UI.RawUI.ForegroundColor = "Green";

#Other Available Color Choices
#Black, DarkBlue, DarkGreen, DarkCyan, DarkRed, DarkMagenta, DarkYellow, Gray, DarkGray, Blue, Green, Cyan, Red, Magenta, Yellow, White

#Change Error Text Color to White (If You Were Using Red for Normal Text)
((Get-Host).PrivateData).ErrorForegroundColor = "White";
#Change Warning and Verbose Text Color to Magenta (If You Were Using Yellow for Normal Text)
#((Get-Host).PrivateData).WarningForegroundColor = "Magenta";
#((Get-Host).PrivateData).VerboseForegroundColor = "Magenta";
#Change Error and Warning Background Color to Gray (If You Were Using Black for Normal Text)
#((Get-Host).PrivateData).ErrorBackgroundColor = "Gray";
#((Get-Host).PrivateData).WarningBackgroundColor = "Gray";

#Clear the Console to Load New Color Settings
Clear-Host;

#Load Exchange If On System
if(Test-Path $env:ExchangeInstallPath\bin\RemoteExchange.ps1)
{
.$env:ExchangeInstallPath\bin\RemoteExchange.ps1;
Connect-ExchangeServer -auto;
Set-ADServerSettings -ViewEntireForest $true;
}

#Change the Prompt Configuration
function prompt
{
#Get the Current Directory
$path = Get-Location;
#Set Prompt for the Computer Name then Directory Path
"PS [$env:computername] $path>";
}

#Change the Window Title
$host.UI.RawUI.WindowTitle = "Dean's PS Goodness";

#Change Location to the Desktop
$dsktop = [Environment]::GetFolderPath("Desktop").ToString();
cd $dsktop;


References:

How to use a PowerShell Profile to simplify tasks
http://www.techrepublic.com/blog/networking/how-to-use-a-powershell-profile-to-simplify-tasks/5393
Managing Exchange 2010 with Remote PowerShell
http://www.mikepfeiffer.net/2010/02/managing-exchange-2010-with-remote-powershell/

No comments: