Sunday, February 20, 2011

Using Windows PowerShell as an IT Pro – Part 5

Using Windows PowerShell as an IT Pro – Part 5

In my last post I talked about how to add snap-ins to your Windows PowerShell session. Now I will show an easy way to open a session that already has the snap-in added so that you don’t have to do it manually each time.
A Windows PowerShell profile is a script that runs when Windows PowerShell starts. You can use the profile as a logon script to customize the environment. You can add commands, aliases, functions, variables, snap-ins, modules, and Windows PowerShell drives. You can also add other session-specific elements to your profile so they are available in every session without having to import or re-create them.
So you can create a new profile or edit an existing one to include whatever changes you wish, such as adding a snap-in. The $Profile automatic variable stores the paths to the Windows PowerShell profiles that are available in the current session.
We will use $Profile to test and see if a profile already exists.
Test-path $Profile
Profile01
If a profile exists then this command will return a value of True. If it doesn’t exist then the command will return a value of False.
To create a new profile we will use the New-Item cmdlet to create a file using the name and path stored in the $Profile variable. The Force parameter will force the command to create the new file no matter what. If you already have a profile you can skip this step.
New-Item -Path $Profile -Type file -Force
Profile02
Now we will edit the new profile using Notepad.
Notepad $Profile
Profile03
Now you can add any commands you want to run when Windows PowerShell opens. I like to keep all of my files for PowerShell in the same place so I will add a command to set the location to that folder. Then I will add a command to add the snap-in that I want to work with.
Profile04
Now every time I start Windows PowerShell it will start in the folder I specified and the snap-in I want will be available without me having to run anything manually.
Using a profile counts the same as running a script. The default Execution Policy in Windows PowerShell prevents all scripts from running, including scripts that you write on the local computer. Windows PowerShell execution policies let you determine the conditions under which Windows PowerShell loads configuration files and runs scripts. I will talk about the different Execution Policies at a later time but to allow your profile to run you can use the following command. This command sets the execution policy to RemoteSigned which requires a digital signature from a trusted publisher on scripts and configuration files that are downloaded from the Internet (including e-mail and instant messaging programs). This policy does not require digital signatures on scripts that have been run and written on the local computer (not downloaded from the Internet).
Set-ExecutionPolicy RemoteSigned
Profile05
In my next post we will look at piping commands.

No comments:

Post a Comment