Run powershell from cmd for command get-hotfix RRS feed

  • Question

  • Guys,

    I need to run a powershell command from cmd.

    I need to run this:

    cmd /s /c powershell "Get-HotFix | select @{Name="InstalledOn"; Expression={"{0:M/d/yyyy}" -f [datetime]$_.InstalledOn.toshortdatestring()}} -First 1 | ft -hidetableheaders"

    Its returning:

    InstalledOn : The term 'InstalledOn' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
    At line:1 char:28
    + Get-HotFix | select @{Name=InstalledOn; Expression={{0:M/d/yyyy} -f [ ...
    +                            ~~~~~~~~~~~
        + CategoryInfo          : ObjectNotFound: (InstalledOn:String) [], CommandNotFoundException
        + FullyQualifiedErrorId : CommandNotFoundException

    Do you have any idea? Can you help me?

    I need to return only the last patch date installed but its to run from the CMD due my software limitation.


    Wednesday, February 19, 2020 9:33 PM

All replies

  • Your syntax is wrong.

    help select-object -online

    Read how to use the syntax correctly and look at the examples.


    • Edited by jrv Wednesday, February 19, 2020 9:45 PM
    Wednesday, February 19, 2020 9:40 PM
  • Get-HotFix |
        select {$_.InstalledOn.toshortdatestring} -First 1


    Wednesday, February 19, 2020 9:50 PM
  • Thanks for the anwer jrv.

    You command is returning

    string ToShortDateString()

    The problem is to run the powershell from CMD, running on powershell is ok but when I try to run from cmd its returning the error above.


    • Edited by Igor Fasano Thursday, February 20, 2020 11:44 AM
    Thursday, February 20, 2020 11:43 AM
  • To run as a CMD parameter you must get the quoting correct.


    Thursday, February 20, 2020 1:33 PM
  • Actually that syntax won't work from the command line.  Place the code in a file and run it from a file.

    This works but drop the CMD.

    powershell -command "Get-HotFix |select -first 1 | ForEach-Object{ $_.InstalledOn.ToString('M/d/yyyy')}"


    Thursday, February 20, 2020 1:53 PM
  • This seems to qavoid most issues:

    start /B  powershell -command "Get-HotFix |select -first 1 | ForEach-Object{ $_.InstalledOn.ToShortDateString()}"


    Thursday, February 20, 2020 2:02 PM