Blog Archives
Powershell: Working with Strings – The basics
| $name = “Justin” $date = get-date #### all double quotes – variables evaluated ## old style concat “hello ” + $name + “, how are you this ” + $date.dayofweek ## powershell way, my favorite “hello $name, how are you this $($date.dayofweek)” ## .net formatting, can be very useful. “hello {0}, how are you this {1}” -f $name,$date.dayofweek ## static method of string class [string]::Concat(“hello “, $name, “, how are you this “, $date.dayofweek) ## here-string – these are cool! @” hello $name how are you this $($date.dayofweek) “@##### single quotes – variables are NOT evaluated in quotes ‘hello ‘ + $name + ‘, how are you this ‘ + $date.dayofweek ‘hello $name, how are you this $($date.dayofweek)’ ‘hello {0}, how are you this {1}’ -f $name,$date.dayofweek [string]::Concat(‘hello ‘, $name, ‘, how are you this ‘, $date.dayofweek)@’ hello $name how are you this $($date.dayofweek) ‘@ |
Powershell has a lot of great ways to work with text and one of my favorite ways is inline. If you have a string with a variable in it that variable will be evaluated as show above with
“hello $name, how are you this $($date.DayOfWeek)”
The output of this is:
hello Justin, how are you this Friday
I didn’t need to do anything special to make this work, and as you can imagine with building larger strings, this can be very helpful. Speaking of larger strings, lets jump down to the Here-String, another great feature of Powershell.
@”
hello $name
how are you this $($date.dayofweek)
“@
To start a Here-String you must end the line with @”, and to end it, the line must start with “@ (Thanks Larry).
whatever…. @” <end of line here>
content of here string in here….
<start of line> @” whatever…..
Great for building email bodies or the such.
Single Quote Vs. Double Quote
The top section of code uses all double quotes, and you’ll notice that the variables are evaluated with in those quotes, where as on the bottom it’s a literal string, meaning nothing is evaluated with in the quotes.
How Evaluation works
There is one thing to keep in mind when evaluating variables with members. As you can see in the code we have two variables, a basic String and a DateTime object. With the string I just use the variable inline and it evaluates, but with the DateTime object I have to wrap it to access a member item, otherwise it evaluates just the $Date.
| “$($date.DayOfWeek)” “$date.DayOfWeek” |
You’ll notice the difference in the output,
Friday
07/01/2011 09:16:31.DayOfWeek
$() evaluates the expression inside of the () before writing it, so you can use it to access members but you can also use it to do other things.
| “2 + 3 = $(2+3)” |
String Format
You can even use the string Format to display info which is like the old C/C++ way. Normally I wouldn’t bother with this method, but it’s great with numbers and dates.
“{0}” –f $date
“{0:MM-yy}” –f $date
Great for building dates for use with filenames. There are 3 parts to the Format Item syntax
{index[,alignment][:formatstring]}
This is also good for building tables. lets say you have a first and last time and you want them to line up well, you can specify the size of the field (using negative for left alignment).
| “-{0,15}-” -f $name “-{0,-15}-” -f $name |
Cool huh?
More info here
A Bonus – Static Members
I’m such a big fan of Get-Member that I have to mention it as much as I can. You’ll notice at the top there is a line for Concat. Would I ever use this? Pretty unlikely, But it shows something cool, Static Member’s.
Get-Member –inputobject “” –Static
This will give you a Strings Static members. We can use Static Members like so
[Class]::StaticMember
[Net.Dns]::GetHostEntry($computer)
Note: System. is assumed in class references!
Hope this helps!
