Question

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

Was it helpful?

Solution

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' 

OTHER TIPS

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:\>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top