Home > Powershell > Pimping your Powershell Profile

Pimping your Powershell Profile

If you work on multiple systems and need to use powershell to manage them but they all have their own separate installation it can drive you mad having so many different console shortcuts to launch.

I have 3 at the moment, VMware vSphere PowerCLI, Citrix XenServer PowerShell SnapIn and NetApp’s Data ONTAP PowerShell Toolkit.

Each one installs slightly differently and then runs separately which is a pain.

VMware vSphere PowerCLI installs a PSSnapin and creates this shortcut:

C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -psc “C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vim.psc1” -noe -c “. \”C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-VIToolkitEnvironment.ps1\””

Citrix XenServer PowerShell SnapIn installs a PSSnapin and creates this shortcut:

"C:\Program Files\Citrix\XenServerPSSnapIn\XenServerPSSnapIn.bat"

which runs

@C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
-PSConsoleFile XenServerPSSnapIn.psc1 -Noexit -Nologo

NetApp’s Data ONTAP PowerShell Toolkit requires you to copy files to the Module Path and then use Import-Module to load the snap-in.

So, Profiles to the rescue……powershell runs a startup script called your profile which is just a text file really.

What I really would like is to have a single profile that launches my session on some computers and starts all 3 snap-ins if they are installed.

That’s not a big ask, is it?

Well, lets start with setting up the profile on this particular computer to launch all 3 snap-ins but be clever and allow it to also be run from anywhere.

To find out your local personal powershell profile just type $profile in your powershell command window.

To edit it just type Notepad $profile

Now, that file may not actually exist so you will need to create one in that path if it does not exist.

I then just put one line in here which points to my actual profile on a network share.

\\fileshare\homedrive$\me\Scripts\Powershell\Profile\Microsoft.PowerShell_profile.ps1

This way on any computer you can choose whether to launch your pimped up shared profile. If you don’t put the line in your local profile it will just launch powershell with only whatever is in the local profile.

I then create and customise my profile on the network share using good old Notepad.

I can create PS Drives which are Powershell mapped drives, create any environment variables required and launch anything I choose.
You can go right ahead and customse the colours and have as much fun as you want.

I can then run some powershell which checks whether these 3 snap-ins are already loaded and if not, checks whether they are installed and if so runs them.
You then run powershell from one shortcut:

%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe

Any future additions to my profile would be made here in one place and I can load as many snap-ins or modules as I like. If they are not installed locally the profile won’t try to load them and you’ll be able to see which ones are loaded when you start powershell.

Here’s my pimped up profile which you can see a screen shot above of it running on a computer with the three plug-ins installed.

# Sets APPDATA environment variable if it doesn't exist
if (! (Test-Path [string]$env:APPDATA) ) {$env:APPDATA = $env:USERPROFILE + "\Application Data"}

# Create Powershell Drives
New-PSDrive -name SCRIPT -psprovider FileSystem -root "\\fileshare\data\IT Department\Team\Scripts"
New-PSDrive -name VMSCRIPT -psprovider FileSystem -root "\\fileshare2\common\IT Department\Projects\VMWare\Scripts"
Set-Location VMSCRIPT:
"You are now PowerShell : " + $env:Username

# VMware vSphere PowerCLI
if (((Get-PSSnapin -Name "VMware.VimAutomation.Core" -ErrorAction SilentlyContinue) -eq $null ) -and ((Get-PSSnapin –registered -Name "VMware.VimAutomation.Core") -ne $null))
{
	Write-Host "Adding VMware vSphere PowerCLI" -ForegroundColor Green
	Add-PSSnapin -name VMware.VimAutomation.Core
	."C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-VIToolkitEnvironment.ps1"
}

# Citrix XenServer PowerShell SnapIn
if (((Get-PSSnapin -Name "XenServerPSSnapIn" -ErrorAction SilentlyContinue) -eq $null ) -and ((Get-PSSnapin –registered -Name "XenServerPSSnapIn") -ne $null))
{
	Write-Host "Adding Citrix XenServer PowerShell SnapIn" -ForegroundColor Green
	Add-PSSnapin XenServerPSSnapIn
	."C:\Program Files\Citrix\XenServerPSSnapIn\Initialize-Environment.ps1"
}

# NetApp Data ONTAP PowerShell Toolkit
$exist = $false
foreach ($modulePath in ${env:PSModulePath}.Split(";"))
{
   if ((Test-Path ($modulePath + "DataONTAP\DataONTAP.dll")) -and !$exist)
   {
	Write-Host "Adding NetApp DataOnTap PowerShell" -ForegroundColor Green
	$exist = $true
	Import-Module DataONTAP
   }
}
Categories: Powershell Tags: ,
 

Time limit is exhausted. Please reload the CAPTCHA.