The PowerShell profile is a script that begins to run if you launch PowerShell. You should utilize it to customise the looks of your console, routinely load PowerShell scripts or modules, and create aliases for cmdlets that you just incessantly use.
This put up will look at the assorted PowerShell Profile areas, describe find out how to create a profile, and offer you some sensible examples.
1. Create A Home windows PowerShell Profile File
Each time you launch a PowerShell session, a normal script referred to as a PowerShell profile executes. The profile for “present person, present host” could also be discovered on the following areas, relying on the working system:
Working System | PowerShell Profile Location |
macOS and Linux | ~/.config/PowerShell/Microsoft.Powershell_profile.ps1 |
Home windows | $HomeDocumentsPowerShellMicrosoft.PowerShell_profile.ps1 |
- Run the Take a look at-Path command listed beneath in PowerShell whereas logged in as an administrator to see if a PowerShell profile is already current.
Take a look at-Path $profile
- The command returned False, indicating that no PowerShell profile is but out there.
- There are a lot of profiles for each the present person and all customers. You might be referring to the ‘Present person – Present Host’ profile, nonetheless, if you talk about the Home windows PowerShell profile.
- There are a lot of profiles for each the present person and all customers. You might be referring to the ‘Present person – Present Host’ profile, nonetheless, if you talk about the Home windows profile.
Profile | Path |
Present Consumer – Present Host | $Residence[My ]DocumentsWindowsPowerShellMicrosoft.PowerShell_profile.ps1 |
All Customers – Present Host | $PSHOMEMicrosoft.PowerShell_profile.ps1 |
Present Consumer – All Hosts | $Residence[My ]DocumentsWindowsPowerShellProfile.ps1 |
All Customers – All Hosts | $PSHOMEProfile.ps1 |
- To create your PowerShell profile, challenge the next New-Merchandise command. The Present Consumer – Present Host profile listing ($profile) will embody a script file created by this command.
New-Merchandise -path $profile -type file –power
Run every of the directions listed beneath to confirm the situation of every person’s PowerShell profile.
- Return the Present Consumer – Present Host $profile’s PowerShell profile path.
$profile
- Get the Present Consumer – All Hosts $profile’s profile
$profile.CurrentUserAllHosts
- Return the All Customers – Present Host $profile profile
$profile.AllUsersCurrentHost
- Return the All Customers – Present Host $profile profile
$profile.AllUsersAllHosts
2. Create A Powershell Profile: Customized Technique
PowerShell profiles are first established with a clean default. It can save you time by utilizing your PowerShell profile to avoid wasting variables, aliases, and instructions that you just typically use.
PowerShell disables script execution by default to cease malicious scripts from harming the system, thus it’s possible you’ll need to change this setting.
To vary the execution coverage (Set-ExecutionPolicy) of PowerShell to RemoteSigned, run the command beneath.
If the script was not produced on the system the place it’s being run, it have to be signed in response to the RemoteSigned coverage. The scripts should have a digital signature with the intention to adjust to this coverage, whether or not they’re downloaded over the Web or by packages like Web Explorer, Outlook, or Messenger.
Set-ExecutionPolicy RemoteSigned
Run one of many following instructions to open your PowerShell profile ($PROFILE) in PowerShell ISE or Notepad however with out producing any output.
# Opens Microsoft PowerShell profile in PowerShell ISE
ise $PROFILE
# Opens PowerShell profile in Notepad
notepad $PROFILE
3. Aliasing Fast Powershell Instructions and Capabilities
The most well-liked customizations are accessibility aliases, which allow you to name PowerShell instructions by any title of your selecting.
Then save the modifications, however don’t but shut the file. Add the next aliases to your PowerShell profile.
# Create aliases for incessantly used purposes.
New-Alias np Notepad.exe
# Create aliases for incessantly used instructions
Set-Alias tn Take a look at-NetConnection
4. Altering the Look and Really feel of the PowerShell Console
With a personalized immediate, you possibly can modify PowerShell’s performance and enhance its interactive expertise.
To replicate the modifications to the default settings, add the next data to your PowerShell profile. After saving the modifications, shut the file.
- PowerShell’s default look is altered by way of the Shade-Console operate beneath, which additionally shows the present date and time.
# Create a operate to vary colours in PowerShell
operate Shade-Console {
$Host.ui.rawui.backgroundcolor = "darkcyan"
$Host.ui.rawui.foregroundcolor = "white"
$hosttime = (Get-ChildItem -Path $PSHOMEPowerShell.exe).CreationTime
$Host.UI.RawUI.WindowTitle = "PowerShell ($hosttime)"
Clear-Host
}
# Calls the Shade-Console operate
Shade-Console
- Execute the command listed beneath to reload your PowerShell profile.
. $profile
The next modifications to your profile shall be mirrored on the PowerShell console.
- To check your community connection, use the tn alias you created in your PowerShell profile moderately than the Take a look at-NetConnection cmdlet. The notepad can be opened by utilizing np.
# Take a look at community connection
TN
# Open notepad
np
5. Giving Parameters Default Values
Use the $PSDefaultParameterValues desire variable moderately than repeatedly getting into a difficult-to-remember parameter worth. The power to outline distinctive values for a cmdlet or operate is a helpful facet of this performance.
The desire variable $PSDefaultParameterValues is a hash desk with entries that include a key and a worth. The sample for keys is cmdlet: parameters and values can both be script blocks or parameters (which may take the type of strings, booleans, or integers).
The $PSDefaultParameterValues desire variable can be utilized usually as follows:
$PSDefaultParameterValues=@{"CmdletName:ParameterName"="DefaultValue"}
See the next instance to make use of the $PSDefaultParameterValues desire variable:
- Run the command beneath to set the Verbose parameter to True for all instructions (*), nevertheless it doesn’t print something. The $PSDefaultParameterValues hash desk will acquire a brand new merchandise.
$PSDefaultParameterValues=@{"*:Verbose"=$True}
- To verify the default parameter variable worth, execute the $PSDefaultParameterValues operate with none parameters.
$PSDefaultParameterValues
TN
- For the reason that Verbose parameter worth is ready to True by default for all instructions, it’s possible you’ll view the verbose particulars because the command executes.