Thursday, May 31, 2012

[Powershell] Creating your own Windows Powershell Profile - Part 1

---------- Go to Part 2 ----------
 What is the Windows Powershell Profile ? The Windows PowerShell profile is a script file that runs when Windows PowerShell starts up. You can put cmdlets, scripts, functions - any valid Windows PowerShell commands - into this script file. Each time you start Windows PowerShell, this script file will run. That means you can use the profile to set up your Windows PowerShell environment. Typically that would be custom console settings and aliases, but use your imagination and you can come up with other things you’d like to customize in PowerShell before you start working with it.

Where is the profile ? What makes the profile a profile and not a regular script file is the name and location of the file. Type this at your PowerShell command prompt:
  1. $profile
In my case, the profile is store in my roaming profile

\\MyServer\folder\HOME02\XXXXX\My Documents\WindowsPowerShell\
but, that built-in variable will return something like this for Windows XP:

C:\Documents and Settings\Account\my documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
Or this for Windows Vista and Windows Server 2008:

C:\Users\kenmyer\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
This is the full path to the file that Windows PowerShell will try to run when it starts. Notice we said “try” to run. Here’s an interesting fact: just because you were able to find the profile doesn’t mean it actually exists. $profile is simply a built-in variable that contains the full path to where the profile will be if there is one; the file doesn’t actually have to exist, and, by default. it doesn’t. If you want a profile to run when you start Windows PowerShell, you need to create this file.

Checking if my profile exists:Just execute this at your PowerShell command prompt:
  1. Test-Path $profile 
  • It return "True" means the profile exist. You can edit it by executing at your PowerShell command prompt:
  1. notepad $profile
  • It return "False" means the profile doesn't exist. You can create it by executing at your PowerShell command prompt:
  1. New-Item -path $profile -type file -force
-path $profile full path set to $profile variable
-type file This tells we’re creating a file.
-force to create the file at the path with no matter.

When done you will be able to edit it with the notepad $profile command. This will open the empty file you have just created in Notepad.



[Powershell] Creating your own Windows Powershell Profile - Part 2 will explain what you can put in your profile - it will be OpsMgr oriented but keep in mind that you can put cmdlets, scripts, functions and any command in powershell.

This posting is provided "AS IS" with no warranties.

No comments:

Post a Comment