{"id":4271,"date":"2009-04-02T17:04:00","date_gmt":"2009-04-02T17:04:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2009\/04\/02\/many-ways-you-can-set-a-variable-value\/"},"modified":"2019-02-18T13:12:44","modified_gmt":"2019-02-18T20:12:44","slug":"many-ways-you-can-set-a-variable-value","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/many-ways-you-can-set-a-variable-value\/","title":{"rendered":"Many&#178; ways you can set a variable value"},"content":{"rendered":"<p>There are many ways to set a variable&#8217;s value.<\/p>\n<p>I just learnt one more yesterday. If you have others, please add comments<\/p>\n<p># Simple<br \/># $ gets the variable, and = will assign it<br \/>$a = 1<\/p>\n<p># With Variable Scope<br \/># The prepend is the scope, and could be global, script, and others<br \/># Useful when you want to keep things in script scope, or share them out in global scope<br \/>$global:a = 1<\/p>\n<p># Complex variable names<br \/># Useful if you have a variable with the name ${Yahoo!Id}<br \/>${a} = 1<\/p>\n<p># Using the Set-Variable cmdlet<br \/># Useful if you want to indirectly set the value<br \/># Has extra parameters like scope<br \/>Set-Variable a 1<\/p>\n<p># Using the variable provider<br \/># Never useful, just cool<br \/>set-item variable:\\a 1<\/p>\n<p># Using the variable provider with provider shortcut<br \/># Useful when you want to do ${env:ProgramFiles(x86)}<br \/>${variable:a} = 1<\/p>\n<p># Using the SessionState (PowerShell APIs)<br \/># Useful when you dont want to access the Runspace Directly<br \/># Useful when you want to set tied variable<br \/># <a href=\"http:\/\/blogs.msdn.com\/powershell\/archive\/2009\/03\/26\/tied-variables-in-powershell.aspx\">http:\/\/blogs.msdn.com\/powershell\/archive\/2009\/03\/26\/tied-variables-in-powershell.aspx<\/a><br \/>$executioncontext.SessionState.PSVariable.Set(&#8216;a&#8217;, 1)<\/p>\n<p># Using references<br \/># Useful if you want to indirectly set the value<br \/>$ref = [ref]$a<br \/>$ref.Value = 10<\/p>\n<p>Have fun,<br \/>Ibrahim Abdul Rahim [MSFT]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are many ways to set a variable&#8217;s value. I just learnt one more yesterday. If you have others, please add comments # Simple# $ gets the variable, and = will assign it$a = 1 # With Variable Scope# The prepend is the scope, and could be global, script, and others# Useful when you want [&hellip;]<\/p>\n","protected":false},"author":600,"featured_media":13641,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[76,33],"class_list":["post-4271","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell","tag-fun-games","tag-language"],"acf":[],"blog_post_summary":"<p>There are many ways to set a variable&#8217;s value. I just learnt one more yesterday. If you have others, please add comments # Simple# $ gets the variable, and = will assign it$a = 1 # With Variable Scope# The prepend is the scope, and could be global, script, and others# Useful when you want [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/4271","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/users\/600"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/comments?post=4271"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/4271\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media\/13641"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media?parent=4271"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=4271"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=4271"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}