Instead of replacing after the fact, you can just use a format specifier that does what you want:
get-date -format 'yyyy-MM-dd HH_mm_ssZ'
Vra
I get the date in powershell. The output has : and i want to replace it to _,
Get-Date -format u
result
2014-05-14 16:26:45Z
I want to replace the : with _
Get-Date -format u | $_ -replace ":","_"
Not working
Oplossing
Instead of replacing after the fact, you can just use a format specifier that does what you want:
get-date -format 'yyyy-MM-dd HH_mm_ssZ'
Ander wenke
The pipeline doesn't work quite like that.
Instead, treat the result of get-date
as an object/variable, and use -replace
on that.
(get-date -Format u) -replace ":","_";
Or, since it's just a string, use the replace()
method
(get-date -Format u).Replace(":","_");
You can do the following:
PS H:\> get-date -format u | % {$_.Replace(":","_")}
2014-05-14 09_41_05Z
PS H:\>