You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
$PSScriptRoot is not populated when used in a default value for a non-mandatory script parameter #4688
Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aResolution-FixedThe issue is fixed.WG-Languageparser, language semantics
Actually, what matters is whether the script is an advanced (cmdlet-like) script or not.
By using a [Parameter(...)] attribute, you're implicitly making your script an advanced script, even without decorating your param(...) block with an explicit [CmdletBinding()] attribute.
Combining an advanced script with invocation via powershell -File is what produces the symptom, as the following simplified script demonstrates:
# Direct invocation works fine.> ./t.ps1
[/Users/jdoe]
# Invocation via the external CLI with -File shows the symptom:> powershell -File ./t.ps1
[]
Were you to omit [CmdletBinding()], the symptom would not surface.
Two asides:
Your version table shows a Windows PowerShell version. In this case, the symptom also surfaces in PowerShell Core. Generally, you should only report issues here that (also) surface in PS Core, using the latest released version. If a symptom surfaces only in Windows PS, report it on uservoice.com.
Please use the fenced code and output blocks provided in the issue template (```powershell ... and ```none ...) - it makes for much more readable representations.
Thanks Michael Klement (@mklement0) for the analysis and simple repro! It's really helpful.
It also repros when you dot source a script cmdlet in powershell, for example:
# use the same 't.ps1'>. .\t.ps1
[]
This is because the automatic variables are not set up before parameter binding for a script cmdlet when they are not going to run in new local scopes. I'm preparing a fix.
I believe we've been trying to use Breaking-Change label on issues/PRs that we believe will have a high chance of impact or that the impact is high if encountered. Agree that technically any bug fix is a breaking change. So as to not dilute this label, I'm removing it from this issue as it's unlikely anyone is expecting the previous behavior.
Steve Lee (@SteveL-MSFT) Please reopen this issue, it still occurs in powershell v7.2.0. Or am i missing something? In which version was the bug fixed?
PS C:\scripts>$PSVersionTable
Name Value
---------
PSVersion 7.2.0
PSEdition Core
GitCommitId 7.2.0
OS Microsoft Windows 10.0.19042
Platform Win32NT
PSCompatibleVersions {1.0,2.0,3.0,4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
PS C:\scripts> powershell -File C:\scripts\test.ps1
hello \world
Ilya (@iSazonov) Because pwsh does not exist on the target machine? Seems like the non core version of powershell doesn't contain the fix for this issue. I should also have posted the version of the runtime called via powershell:
powershell -Command "(Get-Variable PSVersionTable -ValueOnly).PSVersion"
Major Minor Build Revision
-----------------------51190411320
Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aResolution-FixedThe issue is fixed.WG-Languageparser, language semantics
Activity
mklement0 commentedon Aug 28, 2017
Actually, what matters is whether the script is an advanced (cmdlet-like) script or not.
By using a
[Parameter(...)]attribute, you're implicitly making your script an advanced script, even without decorating yourparam(...)block with an explicit[CmdletBinding()]attribute.Combining an advanced script with invocation via
powershell -Fileis what produces the symptom, as the following simplified script demonstrates:Save the following as
t.ps1in the current dir:Were you to omit
[CmdletBinding()], the symptom would not surface.Two asides:
Your version table shows a Windows PowerShell version. In this case, the symptom also surfaces in PowerShell Core. Generally, you should only report issues here that (also) surface in PS Core, using the latest released version. If a symptom surfaces only in Windows PS, report it on uservoice.com.
Please use the fenced code and output blocks provided in the issue template (
```powershell ...and```none ...) - it makes for much more readable representations.daxian-dbw commentedon Aug 30, 2017
Thanks Michael Klement (@mklement0) for the analysis and simple repro! It's really helpful.
It also repros when you dot source a script cmdlet in powershell, for example:
This is because the automatic variables are not set up before parameter binding for a script cmdlet when they are not going to run in new local scopes. I'm preparing a fix.
LaurentDardenne commentedon Feb 16, 2018
Steve Lee (@SteveL-MSFT)
This fix does not have the 'Breaking-Change' label ? Is it correct ?
SteveL-MSFT commentedon Feb 21, 2018
Laurent Dardenne (@LaurentDardenne) agree this is technically a breaking change
iSazonov commentedon Feb 21, 2018
:-) Any bug fix is a breaking change. I guess this label is useless here.
SteveL-MSFT commentedon Feb 21, 2018
Ilya (@iSazonov) valid point, I've been thinking about having different degrees of
Breaking Changelabels as they are not all equaliSazonov commentedon Feb 22, 2018
It make sense only if we want strongly document breaking change buckets in docs and changelog.
SteveL-MSFT commentedon Feb 22, 2018
I believe we've been trying to use
Breaking-Changelabel on issues/PRs that we believe will have a high chance of impact or that the impact is high if encountered. Agree that technically any bug fix is a breaking change. So as to not dilute this label, I'm removing it from this issue as it's unlikely anyone is expecting the previous behavior.FireEmerald commentedon Nov 26, 2021
Steve Lee (@SteveL-MSFT) Please reopen this issue, it still occurs in powershell v7.2.0. Or am i missing something? In which version was the bug fixed?
iSazonov commentedon Nov 26, 2021
FireEmerald I wonder you run Windows PowerShell instead of pwsh
rkeithhill commentedon Nov 26, 2021
RE Ilya (@iSazonov)'s

powershell(Windows PowerShell) vspwsh(PowerShell v6/7) question:FireEmerald commentedon Nov 29, 2021
Ilya (@iSazonov) Because pwsh does not exist on the target machine? Seems like the non core version of powershell doesn't contain the fix for this issue. I should also have posted the version of the runtime called via powershell:
iSazonov commentedon Nov 29, 2021
Yes, Windows PowerShell gets only security fixes, all new features are added only to pwsh.