Automatically Loading PSHyperV Library

Late last year I wrote about getting the Powershell Management Library for Hyper-V (PSHyper-V) up and running on my Hyper-V installation. Although a newer version of the library was released in January, I simply hadn’t gotten around to updating my server.  If I’m honest, beyond the occasional restart following an update (or powercuts – thanks for nothing EDF Energy) all of the Hyper-V servers under my care tend to sit the corner and are generally forgotten about.  Far more attention is paid to the Virtual Machines than the actual host upon which they rely.

One change between the versions of PSHyperV has been the change from a standard script to a powershell module.  As I prefer for the library to be automatically loaded, this required a change to the user’s powershell profile.  The slight fly in the ointment is that I had forgotten how to do this, so here is a quick reprise for my own memory.

1.  Open Powershell.  If you are within a command prompt, type cmd /c start powershell to open a new powershell window.

2.  In Powershell, type $profile.  This will get you the full path to where your profile is stored.  Your profile is a powershell script that executes whenever a Powershell prompt is opened.  The profile will look something liek this:

C:\Users\<your username>\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

3.  Your profile file may or may not exist.  If you have not already used it elsewhere, the chances are that it doesn’t.  To create a new profile file, enter new-item $profile -itemtype file -force in Powershell.

4.  Now open the profile file.  If you are using Server Core or Hyper Server R2, you can still use Notepad (it is present, but missing a few features).  So enter notepad $profile in Powershell.

5.  Within Notepad, enter the Import-Module command for PSHyperV.  If you install PSHyperV using the supplied install.cmd, this path should be like

Import-Module “c:\Program Files\modules\hyperv\hyperv.psd1”

6.  Once done, save the file and exit Notepad.

7.  To test, open a new Powershell prompt and type Get-VM.  If everything has gone as planned, you will get a list of all of the VMs present on your Hyper-V server.

This process will only work for the current logged on user.  If you have multiple user accounts on Hyper-V, you will need to repeat this process for all that require access.

Finally, if you have not already done so you will need to set the execution policy for Powershell.  I’ve found that all you require is RemoteSigned.  To set this, enter set-executionpolicy remotesigned within Powershell.

Powershell Tip #1
In Powershell, type $profile.

PS C:\Program Files\Microsoft\AxFuzzer> $profile

That points to where your profile is stored at.  This is a powershell script that executes upon the start up of any powershell prompt for the current user.  Go ahead and make the file.  In my case, I made a new file at the location by typing this:

new-item $profile -itemtype file -force

Now, open the file and you can put in things like this:
set-executionpolicy unrestricted
. \\meng\shared\powershell\hyperv.ps1
set-executionpolicy remotesigned

Every new powershell prompt that you

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.