In a recent project I was collecting client health data across machines in multiple regions, all with different cultures/languages configured. As part of the collection, I was adding in a date column in my report via PowerShell. I was using the Get-Date command to get the current date, and then applying the native format options to it (-Format).
The challenge I ran into was the date looked different when collected from different machines. On some machines today's date would be 06/21/2023, on others, 06.21.2023, or 06-21-2023. Not very consistent when used in a single report.
Here is the full command I used at first:
$Date = Get-Date -Format "MM/dd/yyyy"
To give you some background info, when using for example the above command, it is basically a wrapper around a .NET method, the ToString() method. When using it this way it will provide different results for different cultures.
However, by providing a culture in the command, you can convert to any format you like, no matter what language the machine is set up for. The below example will always return dates like this: 06/21/2023.
$CurrentDate = Get-Date $culture = [CultureInfo]'en-us' $Date = $CurrentDate.ToString('MM/dd/yyyy', $culture)
Problem solved 🙂