In my particular use-case, I wanted to access the arguments from the profile which ignored the params
, and simply checked for the existence of an argument (essentially a switch).
I have a batch script which runs a ps1 file but with a profile. That profile.ps1
outputs text when it loads. In some scripts, I want to disable the output of that text if I don't need it. For that, I created some simple switches such as the example below.
This is a very basic version, but you could extend it if you wished. Using the 'silent-running' mode an example then...
Inside my batch script:
C:\path\to\pwsh.exe -Command "%~dpn0.ps1" -QuietProfile
PowerShell script (inside profile.ps1
):
[Boolean]$global:QuietProfile = [Boolean]([Environment]::GetCommandLineArgs() -match "-QuietProfile")
if (-not $global:QuietProfile) {
Write-Host "I am some text which should not appear when -QuietProfile is passed"
}
By default (if you just opened pwsh.exe) then the $global:QuietProfile
value will be false and you will see the output as normal.
Obviously you could pass -NoProfile
in your command to prevent the profile being loaded at all, but there may be circumstances where you want some useful functions to load but not everything.