Running PowerShell command from CMD gives positional parameter error [duplicate]












1
















This question already has an answer here:




  • problem with powershell and cmd with pipes

    1 answer




I have this PowerShell command:



Get-WmiObject -Query "Select * from CIM_DataFile Where Extension = 'ost'" |
Select-Object 'Name' |
Out-File C:tempost.txt -Append


But I need to run it form a command prompt. I'm running it like this:



powershell.exe -ExecutionPolicy ByPass -Command "Get-WmiObject -Query "Select * from CIM_DataFile Where Extension = 'ost'" | Select-Object 'Name' | Out-File C:tempost.txt -Append"


I'm getting this error:




Get-WmiObject : A positional parameter cannot be found that accepts argument '*'.
At line:1 char:1
+ Get-WmiObject -Query Select * from CIM_DataFile Where Extension = 'os ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Get-WmiObject], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.GetWmiObjectCommand


How do I run this correctly?










share|improve this question















marked as duplicate by Ansgar Wiechers, TheIncorrigible1, mklement0 powershell
Users with the  powershell badge can single-handedly close powershell questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 14 '18 at 18:59


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 1





    Use single-quotes for the -Query parameter and escape your double-quotes with ^.

    – TheIncorrigible1
    Nov 14 '18 at 17:45








  • 2





    Escape the nested double quotes with backslashes: "Get-WmiObject -Query "Select * from ..." | ..."

    – Ansgar Wiechers
    Nov 14 '18 at 17:50













  • @AnsgarWiechers Why ? I thought the cmd escape was ^

    – TheIncorrigible1
    Nov 14 '18 at 17:56






  • 2





    Don't ask me why. All I can tell you is that ^ is the escape character in CMD most of the time. ;)

    – Ansgar Wiechers
    Nov 14 '18 at 17:57






  • 1





    @TheIncorrigible1: It is PowerShell that requires -escaping of " chars. when processing CLI arguments. cmd.exe ignores the itself, though its treating the following " as a syntax element then brings its own challenges.

    – mklement0
    Nov 14 '18 at 19:07
















1
















This question already has an answer here:




  • problem with powershell and cmd with pipes

    1 answer




I have this PowerShell command:



Get-WmiObject -Query "Select * from CIM_DataFile Where Extension = 'ost'" |
Select-Object 'Name' |
Out-File C:tempost.txt -Append


But I need to run it form a command prompt. I'm running it like this:



powershell.exe -ExecutionPolicy ByPass -Command "Get-WmiObject -Query "Select * from CIM_DataFile Where Extension = 'ost'" | Select-Object 'Name' | Out-File C:tempost.txt -Append"


I'm getting this error:




Get-WmiObject : A positional parameter cannot be found that accepts argument '*'.
At line:1 char:1
+ Get-WmiObject -Query Select * from CIM_DataFile Where Extension = 'os ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Get-WmiObject], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.GetWmiObjectCommand


How do I run this correctly?










share|improve this question















marked as duplicate by Ansgar Wiechers, TheIncorrigible1, mklement0 powershell
Users with the  powershell badge can single-handedly close powershell questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 14 '18 at 18:59


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 1





    Use single-quotes for the -Query parameter and escape your double-quotes with ^.

    – TheIncorrigible1
    Nov 14 '18 at 17:45








  • 2





    Escape the nested double quotes with backslashes: "Get-WmiObject -Query "Select * from ..." | ..."

    – Ansgar Wiechers
    Nov 14 '18 at 17:50













  • @AnsgarWiechers Why ? I thought the cmd escape was ^

    – TheIncorrigible1
    Nov 14 '18 at 17:56






  • 2





    Don't ask me why. All I can tell you is that ^ is the escape character in CMD most of the time. ;)

    – Ansgar Wiechers
    Nov 14 '18 at 17:57






  • 1





    @TheIncorrigible1: It is PowerShell that requires -escaping of " chars. when processing CLI arguments. cmd.exe ignores the itself, though its treating the following " as a syntax element then brings its own challenges.

    – mklement0
    Nov 14 '18 at 19:07














1












1








1









This question already has an answer here:




  • problem with powershell and cmd with pipes

    1 answer




I have this PowerShell command:



Get-WmiObject -Query "Select * from CIM_DataFile Where Extension = 'ost'" |
Select-Object 'Name' |
Out-File C:tempost.txt -Append


But I need to run it form a command prompt. I'm running it like this:



powershell.exe -ExecutionPolicy ByPass -Command "Get-WmiObject -Query "Select * from CIM_DataFile Where Extension = 'ost'" | Select-Object 'Name' | Out-File C:tempost.txt -Append"


I'm getting this error:




Get-WmiObject : A positional parameter cannot be found that accepts argument '*'.
At line:1 char:1
+ Get-WmiObject -Query Select * from CIM_DataFile Where Extension = 'os ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Get-WmiObject], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.GetWmiObjectCommand


How do I run this correctly?










share|improve this question

















This question already has an answer here:




  • problem with powershell and cmd with pipes

    1 answer




I have this PowerShell command:



Get-WmiObject -Query "Select * from CIM_DataFile Where Extension = 'ost'" |
Select-Object 'Name' |
Out-File C:tempost.txt -Append


But I need to run it form a command prompt. I'm running it like this:



powershell.exe -ExecutionPolicy ByPass -Command "Get-WmiObject -Query "Select * from CIM_DataFile Where Extension = 'ost'" | Select-Object 'Name' | Out-File C:tempost.txt -Append"


I'm getting this error:




Get-WmiObject : A positional parameter cannot be found that accepts argument '*'.
At line:1 char:1
+ Get-WmiObject -Query Select * from CIM_DataFile Where Extension = 'os ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Get-WmiObject], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.GetWmiObjectCommand


How do I run this correctly?





This question already has an answer here:




  • problem with powershell and cmd with pipes

    1 answer








powershell cmd escaping quoting






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 18:59









mklement0

131k20245281




131k20245281










asked Nov 14 '18 at 17:33









AlexAlex

173




173




marked as duplicate by Ansgar Wiechers, TheIncorrigible1, mklement0 powershell
Users with the  powershell badge can single-handedly close powershell questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 14 '18 at 18:59


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Ansgar Wiechers, TheIncorrigible1, mklement0 powershell
Users with the  powershell badge can single-handedly close powershell questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 14 '18 at 18:59


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 1





    Use single-quotes for the -Query parameter and escape your double-quotes with ^.

    – TheIncorrigible1
    Nov 14 '18 at 17:45








  • 2





    Escape the nested double quotes with backslashes: "Get-WmiObject -Query "Select * from ..." | ..."

    – Ansgar Wiechers
    Nov 14 '18 at 17:50













  • @AnsgarWiechers Why ? I thought the cmd escape was ^

    – TheIncorrigible1
    Nov 14 '18 at 17:56






  • 2





    Don't ask me why. All I can tell you is that ^ is the escape character in CMD most of the time. ;)

    – Ansgar Wiechers
    Nov 14 '18 at 17:57






  • 1





    @TheIncorrigible1: It is PowerShell that requires -escaping of " chars. when processing CLI arguments. cmd.exe ignores the itself, though its treating the following " as a syntax element then brings its own challenges.

    – mklement0
    Nov 14 '18 at 19:07














  • 1





    Use single-quotes for the -Query parameter and escape your double-quotes with ^.

    – TheIncorrigible1
    Nov 14 '18 at 17:45








  • 2





    Escape the nested double quotes with backslashes: "Get-WmiObject -Query "Select * from ..." | ..."

    – Ansgar Wiechers
    Nov 14 '18 at 17:50













  • @AnsgarWiechers Why ? I thought the cmd escape was ^

    – TheIncorrigible1
    Nov 14 '18 at 17:56






  • 2





    Don't ask me why. All I can tell you is that ^ is the escape character in CMD most of the time. ;)

    – Ansgar Wiechers
    Nov 14 '18 at 17:57






  • 1





    @TheIncorrigible1: It is PowerShell that requires -escaping of " chars. when processing CLI arguments. cmd.exe ignores the itself, though its treating the following " as a syntax element then brings its own challenges.

    – mklement0
    Nov 14 '18 at 19:07








1




1





Use single-quotes for the -Query parameter and escape your double-quotes with ^.

– TheIncorrigible1
Nov 14 '18 at 17:45







Use single-quotes for the -Query parameter and escape your double-quotes with ^.

– TheIncorrigible1
Nov 14 '18 at 17:45






2




2





Escape the nested double quotes with backslashes: "Get-WmiObject -Query "Select * from ..." | ..."

– Ansgar Wiechers
Nov 14 '18 at 17:50







Escape the nested double quotes with backslashes: "Get-WmiObject -Query "Select * from ..." | ..."

– Ansgar Wiechers
Nov 14 '18 at 17:50















@AnsgarWiechers Why ? I thought the cmd escape was ^

– TheIncorrigible1
Nov 14 '18 at 17:56





@AnsgarWiechers Why ? I thought the cmd escape was ^

– TheIncorrigible1
Nov 14 '18 at 17:56




2




2





Don't ask me why. All I can tell you is that ^ is the escape character in CMD most of the time. ;)

– Ansgar Wiechers
Nov 14 '18 at 17:57





Don't ask me why. All I can tell you is that ^ is the escape character in CMD most of the time. ;)

– Ansgar Wiechers
Nov 14 '18 at 17:57




1




1





@TheIncorrigible1: It is PowerShell that requires -escaping of " chars. when processing CLI arguments. cmd.exe ignores the itself, though its treating the following " as a syntax element then brings its own challenges.

– mklement0
Nov 14 '18 at 19:07





@TheIncorrigible1: It is PowerShell that requires -escaping of " chars. when processing CLI arguments. cmd.exe ignores the itself, though its treating the following " as a syntax element then brings its own challenges.

– mklement0
Nov 14 '18 at 19:07












1 Answer
1






active

oldest

votes


















2














You must escape the nested " chars. in your command, which is most robustly done as "" (sic):



PowerShell.exe -c  "Get-WmiObject -Query ""Select * ... 'ost'"" | Select ..."


Caveat: Use of "" works well and robustly with powershell.exe, (and pwsh for PowerShell Core) but not with other programs, such as python, ruby, perl or node.



See the linked answer for a detailed explanation, including how to escape for other programs.






share|improve this answer
































    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    You must escape the nested " chars. in your command, which is most robustly done as "" (sic):



    PowerShell.exe -c  "Get-WmiObject -Query ""Select * ... 'ost'"" | Select ..."


    Caveat: Use of "" works well and robustly with powershell.exe, (and pwsh for PowerShell Core) but not with other programs, such as python, ruby, perl or node.



    See the linked answer for a detailed explanation, including how to escape for other programs.






    share|improve this answer






























      2














      You must escape the nested " chars. in your command, which is most robustly done as "" (sic):



      PowerShell.exe -c  "Get-WmiObject -Query ""Select * ... 'ost'"" | Select ..."


      Caveat: Use of "" works well and robustly with powershell.exe, (and pwsh for PowerShell Core) but not with other programs, such as python, ruby, perl or node.



      See the linked answer for a detailed explanation, including how to escape for other programs.






      share|improve this answer




























        2












        2








        2







        You must escape the nested " chars. in your command, which is most robustly done as "" (sic):



        PowerShell.exe -c  "Get-WmiObject -Query ""Select * ... 'ost'"" | Select ..."


        Caveat: Use of "" works well and robustly with powershell.exe, (and pwsh for PowerShell Core) but not with other programs, such as python, ruby, perl or node.



        See the linked answer for a detailed explanation, including how to escape for other programs.






        share|improve this answer















        You must escape the nested " chars. in your command, which is most robustly done as "" (sic):



        PowerShell.exe -c  "Get-WmiObject -Query ""Select * ... 'ost'"" | Select ..."


        Caveat: Use of "" works well and robustly with powershell.exe, (and pwsh for PowerShell Core) but not with other programs, such as python, ruby, perl or node.



        See the linked answer for a detailed explanation, including how to escape for other programs.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 14 '18 at 20:23

























        answered Nov 14 '18 at 18:20









        mklement0mklement0

        131k20245281




        131k20245281

















            Popular posts from this blog

            Bressuire

            Vorschmack

            Quarantine