{"id":4050,"date":"2013-03-08T00:01:00","date_gmt":"2013-03-08T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2013\/03\/08\/learn-to-use-the-powershell-variable-drive\/"},"modified":"2013-03-08T00:01:00","modified_gmt":"2013-03-08T00:01:00","slug":"learn-to-use-the-powershell-variable-drive","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/learn-to-use-the-powershell-variable-drive\/","title":{"rendered":"Learn to Use the PowerShell Variable: Drive"},"content":{"rendered":"<p><strong>Summary<\/strong><span style=\"font-size: 12px\">: Microsoft Scripting Guy, Ed Wilson, talks about using the Windows PowerShell Variable: drive.<\/span><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Question\" \/>&nbsp;Hey, Scripting Guy! What is a Variable: drive? It sounds somewhat &ldquo;iffy.&rdquo;<\/p>\n<p>&mdash;AS<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Answer\" \/>&nbsp;Hello AS,<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. The Charlotte Windows PowerShell User Group meeting last night was great. The Scripting Wife and I had a great time, as did the other attendees. Windows PowerShell MVP, Jim Christopher, and user group supporter, Brian Wilhite, continue to amaze me at how they manage the group. They are doing a fine job.<\/p>\n<p>This morning I am listening to a CD that the Scripting Wife bought me of <a href=\"http:\/\/en.wikipedia.org\/wiki\/Andr%C3%A9s_Segovia\" target=\"_blank\">Segovia<\/a>. Along with a nice cup of <a href=\"http:\/\/en.wikipedia.org\/wiki\/Gunpowder_green_tea\" target=\"_blank\">Gunpowder green tea<\/a> with jasmine, it creates the perfect Windows PowerShell scripting environment (at least for me).<\/p>\n<h2>Access the Variable: drive to find variables<\/h2>\n<p>To access the Windows PowerShell Variable: drive, I can use the <strong>Get-ChildItem<\/strong> cmdlet and specify the drive name. I can do this from any location. This technique is shown here.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; dir variable:<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Value<\/p>\n<p style=\"padding-left: 30px\">&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8211;<\/p>\n<p style=\"padding-left: 30px\">$&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cls<\/p>\n<p style=\"padding-left: 30px\">?&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; True<\/p>\n<p style=\"padding-left: 30px\">^&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cls<\/p>\n<p style=\"padding-left: 30px\">args&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {}<\/p>\n<p style=\"padding-left: 30px\">ConfirmPreference&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; High<\/p>\n<p style=\"padding-left: 30px\">&lt;&hellip;Output Truncated&hellip;&gt;<\/p>\n<p>In general, however, when I am working with the Variable: drive, I set my working location to that drive as shown here.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; cd variable:<\/p>\n<p style=\"padding-left: 30px\">PS Variable:\\&gt;<\/p>\n<p>To see the variables that are defined in my session, I can use the <strong>Get-ChildItem<\/strong> cmdlet (<strong>dir<\/strong>, <strong>ls<\/strong>, and <strong>gci<\/strong> are aliases). This technique is shown here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6607.HSG-3-8-13-01.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6607.HSG-3-8-13-01.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<h2>Modify variables by using the Variable: drive<\/h2>\n<p>To create a new variable, I can use the <strong>New-Item<\/strong> cmdlet. I must specify the name of the variable and the value to associate with it. This technique is shown here.<\/p>\n<p style=\"padding-left: 30px\">PS Variable:\\&gt; New-Item -Name sample -Value &#8220;sample value&#8221;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Value<\/p>\n<p style=\"padding-left: 30px\">&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8211;<\/p>\n<p style=\"padding-left: 30px\">sample&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sample value<\/p>\n<p>To directly access the value, I call the variable with the <strong>$<\/strong> (dollar sign) in front of the variable name. This is shown here.<\/p>\n<p style=\"padding-left: 30px\">PS Variable:\\&gt; $sample<\/p>\n<p style=\"padding-left: 30px\">sample value<\/p>\n<p>However, if I use <strong>Get-Item<\/strong> to retrieve the variable, I do not use the dollar sign. This is shown here.<\/p>\n<p style=\"padding-left: 30px\">PS Variable:\\&gt; Get-Item sample<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Value<\/p>\n<p style=\"padding-left: 30px\">&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8212;&#8211;<\/p>\n<p style=\"padding-left: 30px\">sample&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sample value<\/p>\n<p>To modify the value that is associated with the variable, I can use the <strong>Set-Item<\/strong> cmdlet as shown here.<\/p>\n<p style=\"padding-left: 30px\">PS Variable:\\&gt; Set-Item sample -Value &#8220;new sample value&#8221;<\/p>\n<p style=\"padding-left: 30px\">PS Variable:\\&gt; Get-Item sample<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Value<\/p>\n<p style=\"padding-left: 30px\">&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8211;<\/p>\n<p style=\"padding-left: 30px\">sample&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; new sample value<\/p>\n<p>If I want to delete the variable, I use the <strong>Remove-Item<\/strong> cmdlet. This technique is shown here.<\/p>\n<p style=\"padding-left: 30px\">PS Variable:\\&gt; Get-Item sample | Remove-Item<\/p>\n<p>I can also use the <strong>Remove-Item<\/strong> cmdlet directly (and not use the pipeline). This is shown here.<\/p>\n<p style=\"padding-left: 30px\">PS Variable:\\&gt; New-Item -Name sample -Value &#8220;sample value&#8221;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Value<\/p>\n<p style=\"padding-left: 30px\">&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8211;<\/p>\n<p style=\"padding-left: 30px\">sample&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sample value<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">PS Variable:\\&gt; Remove-Item sample<\/p>\n<p>AS, that is all there is to using the Windows PowerShell Variable: drive. This also concludes Windows PowerShell Provider Week. Join me tomorrow for the Weekend Scripter when I will talk about finding dynamic parameters for your favorite Windows PowerShell cmdlets.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><strong>Ed Wilson, Microsoft Scripting Guy<\/strong>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about using the Windows PowerShell Variable: drive. &nbsp;Hey, Scripting Guy! What is a Variable: drive? It sounds somewhat &ldquo;iffy.&rdquo; &mdash;AS &nbsp;Hello AS, Microsoft Scripting Guy, Ed Wilson, is here. The Charlotte Windows PowerShell User Group meeting last night was great. The Scripting Wife and I had a great [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[415,3,4,45],"class_list":["post-4050","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-psproviders","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about using the Windows PowerShell Variable: drive. &nbsp;Hey, Scripting Guy! What is a Variable: drive? It sounds somewhat &ldquo;iffy.&rdquo; &mdash;AS &nbsp;Hello AS, Microsoft Scripting Guy, Ed Wilson, is here. The Charlotte Windows PowerShell User Group meeting last night was great. The Scripting Wife and I had a great [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4050","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=4050"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4050\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=4050"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=4050"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=4050"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}