Guy's Scripting Ezine 111 - Profile.ps1 and CmdletsGuy's Scripting Ezine 111 - Profile.ps1 and Cmdlets
♣ This Week's SecretI had a misspent youth playing with AutoExec.bat and Config.sys. Thus I was familiar with files such as profile.ps1, which control PowerShell's startup environment. Nevertheless, I fell into the trap of calling the file profileS.ps1 instead of profile.ps1. The lesson is PowerShell consistently uses singular nouns. Learn from my mistake and remember that it's profile, process and service and not profiles, processes and services. This Week's MissionThis Week's Mission is to persuade PowerShell to read script files. This is an easy task because all you need to do is save regular commands into a file with .ps1 extension. Then you call your file, known as a cmdlet, from the PowerShell command line. However, I would like to start with the special file called profile.ps1 and then move on to cmdlets. Profile.ps1 is a special cmdlet. The only tricky part is creating profile.ps1 in the correct location. It is a mystery to me why the PowerShell_Setup-i386.msi does not create the PSConfiguration folder and the profiles.ps1 file automatically. 1) Enabling cmdlets (Scripts) in the RegistryWithout making the following registry hack you may get an error message: 'The execution of scripts is disabled'. What I did was to change a REG_SZ called ExecutionPolicy from Restricted to UNRestricted. Here is the full path to enable .PowerShell cmdlets in general and profile.ps1 in particular. HKLM\Software\Microsoft\PowerShell\1\ShellIds \Microsoft.Management.Automation.PowerShell Alternatively, use PowerShell's own commands to manipulate the registry: Investigate other settings such as RemoteSigned, try typing set-ExecutionPolicy -? See more about the PowerShell set-ExecutionPolicy command 2) Locating the path for the Profile.ps1 fileMy main problem was locating the precise path shown below. In truth, it was my fault for not paying attention to detail, I was too busy being annoyed that the installation did not create the PSConfiguration sub folder, that I kept creating PSConfiguration in slightly the wrong place. Note: as with all PowerShell nouns, profile.ps1 is the singular. See here for the latest on Profile.ps1
Preparation: Navigate to your Windows directory called, 'All Users', then find either of these sub folders. C:\Documents and Settings\All Users\Documents\ First Task: Create a sub
folder called precisely PSConfiguration for example Second Task: Launch notepad and create a file called precisely profile.ps1. Make sure that you have the correct file extension, .ps1 and not txt or even PowerShell.txt. This is the position, you should now have: C:\Documents and Settings\All
Users\Documents\PSConfiguration\profile.ps1 Third Task: In profile.ps1 just add an instruction to clear the screen. I confess
that I used cls to mask error messages about the phantom "P:\"
drive. On the next line I added a phrase to prove that it's my profile.ps1 which is active and not the default profile.ps1. Fourth Task: Now launch PowerShell. Once you have succeeded in placing your profile.ps1 in the correct path then you can read up about flashy commands to enter into this startup configuration file, profile.ps1. Guy Recommends:
The Free IP Address Tracker (IPAT)
| |||||
Custom Search
|
Guy Recommends: WMI Monitor and It's Free!
|
|
Home Copyright © 1999-2012 Computer Performance LTD All rights reserved Please report a broken link, or an error. | |