{"id":12,"date":"2010-12-21T04:27:32","date_gmt":"2010-12-21T04:27:32","guid":{"rendered":"http:\/\/ryan.witschger.net\/?p=12"},"modified":"2010-12-21T04:27:32","modified_gmt":"2010-12-21T04:27:32","slug":"quick-tip-entering-object-properties-into-a-string-in-powershell","status":"publish","type":"post","link":"http:\/\/www.Get-Blog.com\/?p=12","title":{"rendered":"Quick Tip: Entering Object Properties into a String in PowerShell"},"content":{"rendered":"<p>Often times you want to use an object&#8217;s specific property to let the user know what&#8217;s going on.  The problem is that PowerShell won&#8217;t allow its dot notation within quotes.  When you first try it you might be confused by the results.<\/p>\n<div class=\"codecolorer-container powershell solarized-dark\" style=\"overflow:auto;white-space:nowrap;width:680px;\"><div class=\"powershell codecolorer\"><span class=\"re0\">$Files<\/span> <span class=\"sy0\">=<\/span> <span class=\"kw1\">Get-ChildItem<\/span><br \/>\n<span class=\"st0\">&quot;This directory has $Files.Count files&quot;<\/span><br \/>\n<br \/>\n<span class=\"sy0\">&gt;&gt;<\/span> This directory has Contacts Desktop Documents.Count files<\/div><\/div>\n<p>The reason is that PowerShell is only looking for a variable name and ignoring anything else.  In this case it is converting all the items held in the $Files variable and displaying it as a string.  That makes you tempted to create a variable whose only purpose is to be used in a string on the following line.<\/p>\n<div class=\"codecolorer-container powershell solarized-dark\" style=\"overflow:auto;white-space:nowrap;width:680px;\"><div class=\"powershell codecolorer\"><span class=\"re0\">$Files<\/span> <span class=\"sy0\">=<\/span> <span class=\"kw1\">Get-ChildItem<\/span><br \/>\n<span class=\"re0\">$NumberFiles<\/span> <span class=\"sy0\">=<\/span> <span class=\"re0\">$Files<\/span>.Count<br \/>\n<span class=\"st0\">&quot;This directory has $NumberFiles files&quot;<\/span><br \/>\n<br \/>\n<span class=\"sy0\">&gt;&gt;<\/span> This directory has <span class=\"nu0\">3<\/span> files<\/div><\/div>\n<p>This will certainly get you by, but it adds lines of code and variables that won&#8217;t be used for anything else.  Instead, you can make PowerShell think that you have created a variable simply by surrounding the variable and its dot notation in $() which basically creates a temporary variable which you can work with as you please.<\/p>\n<div class=\"codecolorer-container powershell solarized-dark\" style=\"overflow:auto;white-space:nowrap;width:680px;\"><div class=\"powershell codecolorer\"><span class=\"re0\">$Files<\/span> <span class=\"sy0\">=<\/span> <span class=\"kw1\">Get-ChildItem<\/span><br \/>\n<span class=\"st0\">&quot;This directory has $($Files.Count) files&quot;<\/span><br \/>\n<br \/>\n<span class=\"sy0\">&gt;&gt;<\/span> This directory has <span class=\"nu0\">3<\/span> files<\/div><\/div>\n<p>In this manner you eliminate a useless variable and an excess line of code, but it doesn&#8217;t have to stop there.  You can even place code blocks inside and really shorten things up.  To make our sample code a single line I will use two sets of $().  One to make a temporary variable for Get-ChildItem and one to read the property &#8220;Count&#8221; and send the output to the string.<\/p>\n<div class=\"codecolorer-container powershell solarized-dark\" style=\"overflow:auto;white-space:nowrap;width:680px;\"><div class=\"powershell codecolorer\"><span class=\"st0\">&quot;This directory has $($(Get-ChildItem).count) files&quot;<\/span><br \/>\n<br \/>\n<span class=\"sy0\">&gt;&gt;<\/span> This directory has <span class=\"nu0\">3<\/span> files<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Eliminate extra variables created just for strings by using a $()<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[5],"tags":[],"_links":{"self":[{"href":"http:\/\/www.Get-Blog.com\/index.php?rest_route=\/wp\/v2\/posts\/12"}],"collection":[{"href":"http:\/\/www.Get-Blog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.Get-Blog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.Get-Blog.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.Get-Blog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=12"}],"version-history":[{"count":0,"href":"http:\/\/www.Get-Blog.com\/index.php?rest_route=\/wp\/v2\/posts\/12\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.Get-Blog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=12"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.Get-Blog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=12"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.Get-Blog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=12"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}