0

I have a powershell script that I want to run from the command line (cmd, not powershell):

powershell.exe -NoLogo -NoProfile -ExecutionPolicy ByPass -File "MyScript.ps1" -ScriptParam1 2020 -ScriptParam2 "bla" -ScriptParam3 "blub"

When I paste this code in powershell everything works fine but when I paste it in cmd, powershell starts and prompts me for the script parameters because they're mandatory. Why are those arguments not recognized in cmd?

Here is the content of MyScript

[CmdletBinding()]
param (
    [Parameter(Mandatory)]
    [int] $ScriptParam1,

    [Parameter(Mandatory)]
    [string] $ScriptParam2,

    [Parameter(Mandatory)]
    [string] $ScriptParam3
)

$PSVersionTable
4
  • Worked fine for me. Worked just as well removing -File and the parameter names as well. powershell.exe -NoLogo -NoProfile -ExecutionPolicy ByPass MyScript.ps1 2020 "bla" "blub" Commented Aug 6, 2020 at 16:15
  • Works fine as is in Microsoft Windows [Version 10.0.19041.388] and PowerShell PSVersion 5.1.19041.1 and using the following script body $MyInvocation.BoundParameters Commented Aug 6, 2020 at 16:29
  • What is the content of MyScript.ps1, specifically the param Commented Aug 6, 2020 at 19:35
  • Hmm I just noticed it runs fine on a different machine. I don't have access to the original machine so I can't check which versions are running there. Commented Aug 7, 2020 at 9:51

1 Answer 1

1

Because they are outside command request, meaning, your script call.

Try it this way...

# Contents of hello.ps1
Get-Content -Path 'D:\Scripts\hello.ps1'
# Results
<#
[CmdletBinding(SupportsShouldProcess)]

Param
(
    [Parameter(ParameterSetName="Domain",Mandatory=$true)]
    [string]$ScriptParam1,
    [Parameter(ParameterSetName="Domain",Mandatory=$true)]
    [string]$ScriptParam2,
    [Parameter(ParameterSetName="Domain",Mandatory=$true)]
    [string]$ScriptParam3
)



$host
$PSversionTable
Write-Host 'Hello World'
Get-Date
#>
powershell.exe -NoExit -NoLogo -NoProfile -ExecutionPolicy ByPass "D:\Scripts\hello.ps1 -ScriptParam1 param1 -ScriptParam2 param2 -ScriptParam3 param3"
# Results
<#
Name             : ConsoleHost
Version          : 5.1.19041.1
...

Key   : PSVersion
Value : 5.1.19041.1
...

Hello World
...
#>

powershell.exe -NoExit -NoLogo -NoProfile -ExecutionPolicy ByPass "D:\Scripts\hello.ps1 -ScriptParam1 param1 -ScriptParam2 param2 -ScriptParam3 param3"
# Results
<#
C:\>ver

Microsoft Windows [Version 10.0.19041.388]

C:\>powershell.exe -NoExit -NoLogo -NoProfile -ExecutionPolicy ByPass "D:\Scripts\hello.ps1 -ScriptParam1 param1 -ScriptParam2 param2 -ScriptParam3 param3"


Name             : ConsoleHost
Version          : 5.1.19041.1
...

Key   : PSVersion
Value : 5.1.19041.1
...

Hello World
...


C:\>ver

Microsoft Windows [Version 10.0.19041.388]

C:\>powershell.exe -NoExit -NoLogo -NoProfile -ExecutionPolicy ByPass "D:\Scripts\hello.ps1"

cmdlet hello.ps1 at command pipeline position 1
Supply values for the following parameters:
ScriptParam1: 1
ScriptParam2: 2
ScriptParam3: 3


Name             : ConsoleHost
Version          : 5.1.19041.1
...

Key   : PSVersion
Value : 5.1.19041.1
...

Hello World
...
#>

Parsing happens a bit differently when you are already in a PowerShell instance than when you are starting one from cmd.exe. Cmd.exe and to read it all first, and cmd.exe has no idea what those -ScriptParam* are, thus they are ignored.

As per the example shown.

Sign up to request clarification or add additional context in comments.

5 Comments

Hmm I've tested the same code on a different machine and it runs perfectly fine. Not sure what causes the problem on my other machine. Are there any differences between cmd versions?
Nope, cmd.exe has been the way it is for years now. No real changes. So, did you try my suggestion above on those choking machines?
Not yet. Takes a couple of days until I have access to those again. I'll report back.
No worries and understood.
Alright tested it and it works. Surprisingly, my version now works as well and I have no idea why it didn't before...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.