Building PowerShell 7 Profile Files
Profile files are PowerShell scripts that PowerShell runs at startup. They are easy to create and support a range of deployment scenarios. They enable you to customize your PowerShell environment. See this article on Microsoft’s PowerShell Community blog for more details on PowerShell profile files: https://devblogs.microsoft.com/powershell-community/how-to-make-use-of-powershell-profile-files/.
In this recipe, you examine profile files, download a sample PowerShell profile file, and install it on SRV1
. This profile is just for the console. In a later recipe, you install VS Code and create a VS Code-specific profile.
Getting ready
You run this recipe on SRV1
after you have installed PowerShell 7. You should begin this recipe by opening up a PowerShell 7 console.
How to do it...
- Discovering the profile filenames
$ProfileFiles = $PROFILE | Get-Member -MemberType NoteProperty $ProfileFiles | Format-Table -Property Name, Definition
- Checking for the existence of each PowerShell profile file
Foreach ($ProfileFile in $ProfileFiles){ "Testing $($ProfileFile.Name)" $ProfilePath = $ProfileFile.Definition.split('=')[1] If (Test-Path -Path $ProfilePath){ "$($ProfileFile.Name) DOES EXIST" "At $ProfilePath" } Else { "$($ProfileFile.Name) DOES NOT EXIST" } "" }
- Discovering a Current User/Current Host profile
$CUCHProfile = $PROFILE.CurrentUserCurrentHost "Current User/Current Host profile path: [$CUCHPROFILE]"
- Creating a Current User/Current Host profile for the PowerShell 7 console
$URI = 'https://raw.githubusercontent.com/doctordns/PacktPS72/master/' + 'scripts/goodies/Microsoft.PowerShell_Profile.ps1' New-Item $CUCHProfile -Force -WarningAction SilentlyContinue | Out-Null (Invoke-WebRequest -Uri $URI).Content | Out-File -FilePath $CUCHProfile
- Exiting from the PowerShell 7 console
Exit
- Restarting the PowerShell 7 console and viewing the profile output at startup
Get-ChildItem -Path $PROFILE
How it works...
In step 1, you use the $Profile
built-in variable to obtain the filenames of the four profile files in PowerShell, with output like this:
Figure 1.19: Obtaining the PowerShell profile filenames
In step 2, you check to see which, if any, of the four profiles exist, with output like this:
Figure 1.20: Checking for the existence of the profile files
The profile file most IT pros use is the Current User/Current Host profile (aka $Profile
). In step 3, you discover the filename for this profile file, with the following output:
Figure 1.21: Viewing the name of the Current User/Current Host profile file
In step 4, you download a sample PowerShell console profile file from GitHub. This step creates no output. After making a new profile file, in step 5, you exit PowerShell 7. After restarting the console, in step 6, you view the details of this profile file. The output from this step looks like this:
Figure 1.22: Viewing the name of the Current User/Current Host profile file
There’s more...
In step 1, you view the built-in profile filenames. As you can see, PowerShell has four profile files you can use. These files enable you to configure a given PowerShell host or all hosts for one or all users. As you can see in step 2, none of the four profile files exist by default.
In step 4, you create the Current User/Current Host profile file based on a code sample you download from GitHub. This profile file is a starting point and demonstrates many things you can do in a profile file.
In step 6, you view the profile file you created earlier. Also, notice that the prompt has changed – the current working directory when you start PowerShell is now C:\Foo
.