Introduction:
PowerShell profiles can be used to create a custom and consistent environment for your steps to run on in a Windows executor job. Profiles can include. variables, commands, functions and other elements that are needed.
Prerequisites:
Please ensure that you have a PowerShell profile available within your Windows job. This can be checked out from your repository, or generated dynamically with a script inside of the job itself.
Instructions:
Create PowerShell profile .ps1 file
Write-Host "Loading custom PowerShell profile" $env:TEST_VAR = "Here is a sample string"
Copy profile to $PROFILE.AllUsersCurrentHost
Copy-Item .\MyPowershellProfile.ps1 $PROFILE.AllUsersCurrentHost
Set step shell to powershell.exe
The shell can be set at the step level or job level by including:
shell: powershell.exe
Outcome:
Setting $PROFILE.AllUsersCurrentHost to the custom profile will execute it at the beginning of every step. This allows you to re-use anything defined within the profile, and reduces the amount of redundant code in your config.yml file.
Additional Notes:
Here is a full sample config.yml showing how to implement this:
version: 2.1orbs: windows: circleci/[email protected]: create_profile: steps: - run: name: Set PowerShell profile command: | Copy-Item .\MyPowershellProfile.ps1 $PROFILE.AllUsersCurrentHost jobs: build: machine: image: windows-server-2022-gui:current resource_class: windows.medium shell: powershell.exe steps: - checkout - create_profile - run: name: Test step with profile shell: powershell.exe command: | Write-Host "TEST_VAR is $env:TEST_VAR" - run: name: Test step with no profile shell: powershell.exe -NoProfile command: | Write-Host "TEST_VAR is $env:TEST_VAR" workflows: my-workflow: jobs: - build