Quantcast
Channel: PowerShell.org » All Posts
Viewing all articles
Browse latest Browse all 13067

Reply To: How to remove characters from output

$
0
0

Just about any time you see that @{Days=88} format, it’s PowerShell’s string representation of an object which has a Days property, and usually it comes from the exact problem in your current code:

$b = New-TimeSpan -Start $y -End $a | Select Days

That “| Select Days” bit gives you an object with a Days property, rather than assigning the value of the timespan’s Days property to $b. There are two options here, either of which are fine for your particular bit of code:

# use parentheses and dot-notation to access the Days property of the timespan
$b = (New-TimeSpan -Start $y -End $a).Days

# use Select-Object's -ExpandProperty parameter instead of -Property (which is its default if you pass a property name by position):
$b = New-TimeSpan -Start $y -End $a | Select -ExpandProperty Days

The -ExpandProperty option is more appropriate in some cases if you’re dealing with multiple piped objects and want to avoid having to hold the entire result set in memory (or if you’re using PowerShell 2.0). For this specific case, where you know there will always be exactly one timespan object in play, I’d just use (New-TimeSpan -Start $y -End $a).Days , personally.


Viewing all articles
Browse latest Browse all 13067

Trending Articles