PowerShell: Output the Result of a Command and Assign it to a Variable in One Line

As of today, there is one month left until the PowerShell Summit North America 2014. I tweeted something out last night and thought I would write a quick blog about it since I often find myself looking for a tweet months later when I can’t remember how I did something that I previously tweeted out.

This tweet used all 140 characters that twitter allows:


This portion of the command is assigned to a variable named $i (technically it’s assigned to a variable named “i”):


It determines the amount of time until 9am on April 28, 2014 in the GMT -7 timezone (the current timezone for Seattle) so you can run this command from any time zone and it will display accurate information.

Here I’ve assigned the value to the variable $i and displayed the value of the days property, all in one line:


At this point $i contains the following value:


When you surround it by quotation marks you end up with a hot mess:


Enclosing the command in dollar sign parenthesis resolves the issue:


Now you can use the $i variable on the same line to display the hours in addition to the days since you wouldn’t want to show up to the PowerShell Summit too many hours early:



1 Comment

  1. theotherkidd

    Nice. I’ve been working with new-timespan a bit lately. Thanks for the info


Leave a Reply to theotherkidd Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: