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:

2014-03-28_14-50-59

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

2014-03-28_14-30-45

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:

2014-03-28_14-38-16

At this point $i contains the following value:

2014-03-28_14-41-48

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

2014-03-28_14-43-41

Enclosing the command in dollar sign parenthesis resolves the issue:

2014-03-28_14-47-47

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:

2014-03-28_14-24-56

µ

1 Comment

  1. theotherkidd

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

    Reply

Leave a Reply

%d bloggers like this: