When I execute a function that returns a datetime (like GETDATE()
) in SQL Server Management Studio the output is formatted in the user's "culture" (region/language settings).
However when that same query is executed in Powershell through the Invoke-Sqlcmd
cmdlet the output is formatted in US date format (which I assume is the Invariant culture).
Other Cmdlets like Get-Date
return the datetime in the CurrentCulture.
How can I ensure that the output of Invoke-Sqlcmd
respects the CurrentCulture settings?
Minimal code sample to demonstrate
$result = Invoke-Sqlcmd -query "select GETDATE() as datetime" -ServerInstance ...
$now = Get-date -format "yyyy-MM-dd HH:mm:ss"
if ( $result.datetime -eq $now) {
Write-output "ok"
} else {
Write-warning "dates mismatch '$($result.datetime)' should be '$now'"
}
The warning shows the first datetime displayed in US format and the second datetime in the format specified