locked
Simple command failing from command prompt RRS feed

  • Question

  • Hi All.

    I need to run a command from the command prompt, as seen below. For some reason, it seems to ba failing to recongnise some of the powershell scripts. I am not sure what is happening.

    if (test-path 'D:\logs\jobs') { Get-ChildItem "D:\logs\jobs" -Recurse -Include *.out, *.txt | Where-Object{($_.LastWriteTime -le (Get-Date).Addminutes(-1))} | select $_.Name }

    If one however stripped off all the code after |, it seems to work from DOS.

    Any insight into the behaviour ?

    Thanks in advance.

    Tuesday, January 27, 2015 3:18 PM

Answers

  • You can't do it that way. If you want to run this from cmd, you'd need to use -Command parameter of powershell.exe.

    Why not just launch the PowerShell console?


    EDIT: Also, make sure you see VGSandz's post on why your select will fail.

    Don't retire TechNet! - (Don't give up yet - 13,085+ strong and growing)

    • Edited by Mike Laughlin Tuesday, January 27, 2015 5:22 PM
    • Marked as answer by AnnaWY Monday, February 9, 2015 5:49 AM
    Tuesday, January 27, 2015 5:21 PM
  • Hi MrFlinstone,

    As Mike suggested, we can use the -command parameter in powershell.exe, please refer to the cmd below:

    powershell.exe -command "& {get-service | Select Name}"

    More information about PowerShell.exe Command-Line Help:

    https://technet.microsoft.com/en-us/library/hh847736.aspx

    If there is anything else regarding this issue, please feel free to post back.

    If you have any feedback on our support, please click here.

    Best Regards,

    Anna Wang

    TechNet Community Support


    Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Support, contact tnmff@microsoft.com

    • Edited by AnnaWY Wednesday, January 28, 2015 1:31 AM
    • Proposed as answer by Mike Laughlin Wednesday, January 28, 2015 2:01 AM
    • Marked as answer by AnnaWY Monday, February 9, 2015 5:49 AM
    Wednesday, January 28, 2015 1:30 AM

All replies

  • What do you mean by 'failing to recongnise some of the powershell scripts'?

    Errors?

    Also, what do you mean by 'it seems to work from DOS.'?


    Don't retire TechNet! - (Don't give up yet - 13,085+ strong and growing)

    Tuesday, January 27, 2015 3:56 PM
  • Hi,

    I was able to reproduce the same thing, however..

    get-service | Select $_.Name 

    returns everythin, where as

    get-service | Select Name

    gives just the names.. def. it has to do with the syntax.

    may be from your code, the last part after | can be

    select Name

    Regards,

    V


    Venu

    Tuesday, January 27, 2015 4:28 PM
  • One thing that I forgot to mention guys, this is how I am running the command.

    powershell.exe get-service | Select $_.Name

    I get the error below from command prompt.

    powershell.exe get-service | Select $_.Name
    'Select' is not recognized as an internal or external command,
    operable program or batch file.

    Tuesday, January 27, 2015 5:18 PM
  • You can't do it that way. If you want to run this from cmd, you'd need to use -Command parameter of powershell.exe.

    Why not just launch the PowerShell console?


    EDIT: Also, make sure you see VGSandz's post on why your select will fail.

    Don't retire TechNet! - (Don't give up yet - 13,085+ strong and growing)

    • Edited by Mike Laughlin Tuesday, January 27, 2015 5:22 PM
    • Marked as answer by AnnaWY Monday, February 9, 2015 5:49 AM
    Tuesday, January 27, 2015 5:21 PM
  • Hi MrFlinstone,

    As Mike suggested, we can use the -command parameter in powershell.exe, please refer to the cmd below:

    powershell.exe -command "& {get-service | Select Name}"

    More information about PowerShell.exe Command-Line Help:

    https://technet.microsoft.com/en-us/library/hh847736.aspx

    If there is anything else regarding this issue, please feel free to post back.

    If you have any feedback on our support, please click here.

    Best Regards,

    Anna Wang

    TechNet Community Support


    Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Support, contact tnmff@microsoft.com

    • Edited by AnnaWY Wednesday, January 28, 2015 1:31 AM
    • Proposed as answer by Mike Laughlin Wednesday, January 28, 2015 2:01 AM
    • Marked as answer by AnnaWY Monday, February 9, 2015 5:49 AM
    Wednesday, January 28, 2015 1:30 AM