The Powershell profile file allows you to create custom functions for common tasks. These functions are loaded automatically when you launch a shell, so they are always available to you.
The PowerShell console supports four basic profiles, listed below in precedence order. The first profile has the highest precedence. I just use the All Users, All Hosts location for mine.
Description | Path |
---|---|
All Users, All Hosts | Windows – $PSHOME\Profile.ps1 Linux – /usr/local/microsoft/powershell/7/profile.ps1 macOS – /usr/local/microsoft/powershell/7/profile.ps1 |
All Users, Current Host | Windows – $PSHOME\Microsoft.PowerShell_profile.ps1 Linux – /usr/local/microsoft/powershell/7/Microsoft.Powershell_profile.ps1 macOS – /usr/local/microsoft/powershell/7/Microsoft.Powershell_profile.ps1 |
Current User, All Hosts | Windows – $Home\[My ]Documents\PowerShell\Profile.ps1 Linux – ~/.config/powershell/profile.ps1 macOS – ~/.config/powershell/profile.ps1 |
Current user, Current Host | Windows – $Home\[My ]Documents\PowerShell\Microsoft.PowerShell_profile.ps1 Linux – ~/.config/powershell/Microsoft.Powershell_profile.ps1 macOS – ~/.config/powershell/Microsoft.Powershell_profile.ps1 |
Here’s an example of how the functions should be formatted. There are two here, “Profile” and “SKU”, but you can have as many as you like.
# This function will edit my profile file in ISE
Function Profile {ise $profile.AllUsersAllHosts}
# This furction will retrieve the Stock Keeping Unit code from a device
Function SKU {
Clear-Host
Write-Host
$Machine = Read-Host -Prompt 'Input the machine name'
$SKU = Invoke-Command -ComputerName $Machine -ScriptBlock {(Get-ItemProperty -path 'HKLM:\hardware\description\system\bios' -name SystemSKU).SystemSKU}
Write-Host
Write-Host SKU is $SKU
Write-Host}
To invoke a function, simply type the function name in a shell. If I type “SKU” in a shell, I will be asked for a machine name, and then it will spit out the SKU from that device.
That’s all there is to it. What are some of your favorite scripts to bake into a profile file? Comment below!