# Need help on date variable

**URL:** <https://forums.powershell.org/t/need-help-on-date-variable/13166>\
**Category:** PowerShell Help\
**Created:** [September 19, 2019, 7:35am UTC](https://forums.powershell.org/t/need-help-on-date-variable/13166 "2019-09-19T07:35:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![roose](https://avatars.discourse-cdn.com/v4/letter/r/ce73a5/32.png) [@roose](https://forums.powershell.org/u/roose)\
**Post date:** [September 19, 2019, 7:35am UTC](https://forums.powershell.org/t/need-help-on-date-variable/13166/1 "2019-09-19T07:35:55Z")

</div>

Hi All,

I am still a newbie in PowerShell, and I normally search for existing scripts, study it and modify it to my requirements. Right now, I have been testing a script, run thru Task Scheduler, that will provide me successful RDP login information from our servers, and save the result to a CSV file. However, I have been stuck all afternoon on a date variable that will provide the start time in my filter.

The script will work if put the date itself on the variable, but fails everytime I try to use the Get-Date:

#$StartDate = (Get-Date).AddDays(-7).ToString(“dd-MMM-yyyy”) #–\> does not work  
#$StartDate = (Get-Date).AddDays(-7) #–\> does not work  
$StartDate = “1-Sep-2019” #–\> Works  
$StartDate = “9/1/2019” #–\> Works

Can anyone help to review this code, and for the life of me, I can’t understand why using Get-Date does not work. Here is the full script I am trying to run, and thanks in advance for any hint on how to resolve this:

```
Start-Transcript -path "D:\temp\Get-User-Logins_$(Get-Date -f yyyyMMddHHmm).log"

$Computers = Get-Content "D:\temp\Server list.txt"

Write-Output "Processing the computers"

$LogEntries = Invoke-Command -Computername $Computers -Authentication NegotiateWithImplicitCredential -ThrottleLimit 10 -ErrorAction "SilentlyContinue" -Scriptblock {
    # Get the date 7 days ago as start date
	#$StartDate = (Get-Date).AddDays(-7).ToString("dd-MMM-yyyy") #--> does not work
	#$StartDate = (Get-Date).AddDays(-7) #--> does not work
	$StartDate = "1-Sep-2019" #--> Works
	$StartDate = "9/1/2019" #--> Works
	
    $LogOutput = @()

    $LogFilter = @{
        LogName = 'Microsoft-Windows-TerminalServices-LocalSessionManager/Operational'
        ID = 22 
        StartTime = $StartDate
    }

    $LogOutput = Get-WinEvent -FilterHashtable $LogFilter

    $LogOutput | Foreach { 
        $entry = [xml]$_.ToXml()
        [array]$EVOutput += New-Object PSObject -Property @{
            TimeCreated = $_.TimeCreated
            User = $entry.Event.UserData.EventXML.User
            IPAddress = $entry.Event.UserData.EventXML.Address
            EventID = $entry.Event.System.EventID
			EventRecordID = $entry.Event.System.EventRecordID
            ServerName = $env:COMPUTERNAME
        }        
    } 

    $EVOutput
}

Write-Output "Writing the output to the file"

$FilteredOutput += $LogEntries | Select ServerName, TimeCreated, User, IPAddress, EventRecordID, @{Name='Action';Expression={
            if ($_.EventID -eq '22'){"Shell start"}
    }
}

    $FilePath = "D:\temp\$(Get-Date -f yyyyMMddHHmm)_RDP_Report.csv"
    $FilteredOutput | Sort -Property ServerName, TimeCreated | Export-Csv $FilePath -NoTypeInformation

Write-Output "Writing File: $FilePath"
Write-Output "Done!"

Stop-Transcript
```

---

<div class="post-metadata">

**Author:** ![kvprasoon](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kvprasoon/32/4449_2.png) [@kvprasoon](https://forums.powershell.org/u/kvprasoon)\
**Post date:** [September 25, 2019, 1:44am UTC](https://forums.powershell.org/t/need-help-on-date-variable/13166/2 "2019-09-25T01:44:05Z")

</div>

Looks like StartTime key doesn’t take DateTime object, but only string.

Can you raise an issue using below link.

> **[Issues · MicrosoftDocs/PowerShell-Docs](https://github.com/MicrosoftDocs/PowerShell-Docs/issues?utf8=%E2%9C%93&amp%3Bq=%22c3049b3f-f46c-b334-d778-69ae9d50257c%22&amp%3Bin=body)**
>
> The official PowerShell documentation sources. Contribute to MicrosoftDocs/PowerShell-Docs development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:31pm UTC](https://forums.powershell.org/t/need-help-on-date-variable/13166/3 "2024-05-16T20:31:18Z")

</div>


