1. Launch PowerShell with administrative permissions. | Some of the steps will not work correctly if PowerShell is not started using Run As Administrator. |
2. Create the global profile:
PS C:\> notepad c:\windows\system32\ windowspowershell\v1.0\profile.ps1
| If the file doesn’t exist, Notepad prompts you to create the file. Click Yes. |
3. Change the PowerShell starting folder:
| Because cd is an alias, you can also enter cd c:\scripts.
Note
The path must exist for this to work. In other words, if c:\scripts isn’t a valid folder, PowerShell gives an error when it launches.
|
4. Add an alias to the profile in Notepad:
| Creates an alias called gh that executes get-help. After this alias is created, you can use the following line to get help on the set-alias command:
PS C:\> gh get-alias |
5. Add a function to the profile:
function get-topprocesses { get-process | sort-object -property ws -descending | select-object -first 10 | out-gridview }
| After this is added to the profile, you can enter the following line at the PowerShell prompt:
PS C:\> get-topprocesses to run the function.
Tip
You can name the function anything you like. For example, you can name it something shorter such as gtp, which is short for get top processes. Additionally, you can have multiple lines of code in the function.
|
6. Press Ctrl+S to save the file. | Saves the profile. |
7. Close and restart PowerShell. | You see the default path is changed to c:\scripts. |
8. Run the alias from the profile:
| Runs the get-help command. |
9. Run the function from the profile:
| Runs the get-topprocesses function command.
Your display looks similar to Figure 1. |
10. Run other scripts from the profile:
. path-script.ps1 . c:\scripts\setenvironment.ps1
| You can run other scripts from within the PowerShell profile by typing a dot, a space, and the path to the script.
Tip
If there is not a space after the dot, the command fails and you see an error message when PowerShell is launched.
|