Below is the singe line command I am trying to run from command prompt.

powershell -command "Get-Date (Get-WmiObject -Class win32_reliabilityRecords -filter \"sourcename = 'Microsoft-Windows-WindowsUpdateClient'\" -ErrorAction SilentlyContinue | select @{LABEL = 'date';EXPRESSION = {$_.ConvertToDateTime($_.timegenerated)}} | select -first 01).date -format yyyyMMdd | Out-File -FilePath C:\temp\winupdatelatest.txt"

Error : Get-Date : Cannot bind parameter 'Date' to the target. Exception setting "Date": "Cannot convert null to type "System.DateTime"." At line:1 char:10

  • Get-Date (Get-WmiObject -Class win32_reliabilityRecords -filter "sour ...
  •      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    • CategoryInfo : WriteError: (:) [Get-Date], ParameterBindingException
    • FullyQualifiedErrorId : ParameterBindingFailed,Microsoft.PowerShell.Commands.GetDateCommand

Thank for your help

  • 2
    So what is the error?
    – DavidPostill
    Aug 8 '21 at 15:10

This is your command, where each line is a new parameter:

"Get-Date (Get-WmiObject -Class win32_reliabilityRecords -filter "sourcename
'Microsoft-Windows-WindowsUpdateClient'" -ErrorAction SilentlyContinue |select @{LABEL = 'date';EXPRESSION = {$_.ConvertToDateTime($_.timegenerated)}} | select -first 01).date -format yyyyMMdd | Out-File -FilePath C:\temp\datelatest.txt"

Basically, your problem is that because you use " ... " ... " ... " some part of the command is seen as a parameter and other part is not.

The easiest solution by far is to put everything inside a .ps1 file and just use:

powershell -file myfile.ps1 -ExecutionPolicy Unrestricted

But if you really have to use this command, then use """ as substiture for one ". Your command would become:

powershell -command "Get-Date (Get-WmiObject -Class win32_reliabilityRecords -filter 
"""sourcename = 'Microsoft-Windows-WindowsUpdateClient'""" -ErrorAction SilentlyContinue 
|select @{LABEL = 'date';EXPRESSION = {$_.ConvertToDateTime($_.timegenerated)}} 
|select -first 01).date -format yyyyMMdd | Out-File -FilePath C:\temp\datelatest.txt"
  • Thanks, i try above but i am getting below error The string is missing the terminator: ". + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : TerminatorExpectedAtEndOfString Aug 8 '21 at 15:34
  • Corrected my answer.
    – LPChip
    Aug 8 '21 at 19:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.