{"id":10961,"date":"2012-03-04T00:01:00","date_gmt":"2012-03-04T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2012\/03\/04\/keep-your-powershell-versions-straight-and-avoid-errors\/"},"modified":"2012-03-04T00:01:00","modified_gmt":"2012-03-04T00:01:00","slug":"keep-your-powershell-versions-straight-and-avoid-errors","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/keep-your-powershell-versions-straight-and-avoid-errors\/","title":{"rendered":"Keep Your PowerShell Versions Straight and Avoid Errors"},"content":{"rendered":"<p><b>Summary<\/b>: Learn how to keep your Windows PowerShell versions straight, and avoid errors while using a one-line command to add computers to domain.<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. Earlier this week I wrote <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2012\/02\/29\/use-powershell-to-replace-netdom-commands-to-join-the-domain.aspx\" target=\"_blank\">Use PowerShell to Replace netdom Commands to Join the Domain<\/a><i>. <\/i>It turned out to be a very popular post. To be honest, however, when I started writing that blog, I intended to show that it could be done as a one-liner. This is because I have been using Windows Server &ldquo;8&rdquo; Beta for long time now, and I was used to some of the new parameters in the <b>Add-Computer <\/b>cmdlet.<\/p>\n<p>But I will predict that for the next several months, things are going to be a bit squirrely as people attempt to balance working with Windows PowerShell&nbsp;2.0 with the various iterations of Windows PowerShell&nbsp;3.0. It happened in the transition between Windows PowerShell&nbsp;1.0 to Windows PowerShell&nbsp;2.0, and I am sure it will happen again this time around.<\/p>\n<p>To be sure, Windows PowerShell&nbsp;3.0 in <a href=\"http:\/\/www.microsoft.com\/en-us\/server-cloud\/windows-server\/v8-default.aspx\" target=\"_blank\">Windows Server &#8220;8&#8221; Beta<\/a> brings much goodness to the table, and it also simplifies syntax in a great many areas. Don&rsquo;t panic, though. If you don&rsquo;t know if something you wrote in Windows PowerShell&nbsp;3.0 will work in Windows PowerShell&nbsp;2.0, just go ahead and add the <b>Requires<\/b><i> <\/i>statement to your script, and you will be safe. The use of the <b>Requires<\/b><i> <\/i>statement is shown here.<\/p>\n<p style=\"padding-left: 30px\">#Requires -version 3.0<\/p>\n<p>Yep, you are seeing that right; it is preceded with a pound character (the normal comment character). When I attempt to run a script that uses new functionality, and I know that the new functionality only resides in a specific version of Windows PowerShell, I add the <b>Requires<\/b><i> <\/i>statement to the first line in the script. When the script runs on a down-level system, a message displays. The use of the <b>Requires<\/b><i> <\/i>statement, and the accompanying message are shown in the image that follows.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1651.hsg-3-4-12-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1651.hsg-3-4-12-01.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>Keep in mind that the <b>Requires<\/b><i> <\/i>statement only works in scripts, not in functions, cmdlets, or snap-ins. In addition, when developing in the Windows PowerShell ISE, if the script is not saved, it is not considered a script. Therefore, it will not work. In the image that follows, an error appears stating that a <i>ComputerName <\/i>parameter cannot be found, instead of the version information that was presented in the previous image.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0601.hsg-3-4-12-02.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0601.hsg-3-4-12-02.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>Removing a computer from the domain in Windows Server &ldquo;8&rdquo; Beta is a one-line command. It also reboots the computer. In addition, the <i>ComputerName <\/i>parameter permits the command to accept an array of remote computer names. Here is the basic command to remove a computer from the domain, join a workgroup called myworkgroup, and reboot.<\/p>\n<p style=\"padding-left: 30px\">Remove-Computer -computername win8c5 -workgroup myworkgroup &ndash;restart<\/p>\n<p>As shown in the image that follows, using the <b>Remove-Computer <\/b>cmdlet without the <i>Force <\/i>switched parameter causes a warning message to appear. With the <i>Force <\/i>switched parameter in effect, no warning appears.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7343.hsg-3-4-12-03.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7343.hsg-3-4-12-03.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>Now&hellip;to get back to adding a computer to a domain by using Windows PowerShell. In Windows PowerShell&nbsp;3.0, the <b>Add-Computer <\/b>cmdlet gains additional parameters. One useful parameter is the <i>Restart <\/i>parameter. This permits the use of a one-line command to add a computer to the domain. The use of this feature is shown here. (The following command is a single command; I have not added any line continuation characters to it.)<\/p>\n<p style=\"padding-left: 30px\">#Requires -version 3.0<\/p>\n<p style=\"padding-left: 30px\">Add-Computer -DomainName iammred -Credential iammred\\administrator -restart -OUPath &#8216;ou=charlotte,dc=iammred,dc=net&#8217;<\/p>\n<p>The image that follows illustrates using the previous command to add a computer to the domain.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7522.hsg-3-4-12-04.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7522.hsg-3-4-12-04.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>If you need to rename the computer while adding it to the domain, the command would appear as follows:<\/p>\n<p style=\"padding-left: 30px\">Add-Computer -DomainName iammred -Credential iammred\\administrator -restart -OUPath &#8216;ou=charlotte,dc=iammred,dc=net&#8217; &ndash;newname mynewcomputername<\/p>\n<p>Keep in mind that Windows PowerShell&nbsp;3.0 and Windows Server &ldquo;8&rdquo; Beta are beta software, and as such the features will change. But I hope you will download them and let us know how you like them. There is some good stuff here.<\/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><b>Ed Wilson, Microsoft Scripting Guy<\/b>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Learn how to keep your Windows PowerShell versions straight, and avoid errors while using a one-line command to add computers to domain. Microsoft Scripting Guy, Ed Wilson, is here. Earlier this week I wrote Use PowerShell to Replace netdom Commands to Join the Domain. It turned out to be a very popular post. To [&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":[7,46,51,3,4,61,45],"class_list":["post-10961","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-active-directory","tag-computer-accounts","tag-getting-started","tag-scripting-guy","tag-scripting-techniques","tag-weekend-scripter","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Learn how to keep your Windows PowerShell versions straight, and avoid errors while using a one-line command to add computers to domain. Microsoft Scripting Guy, Ed Wilson, is here. Earlier this week I wrote Use PowerShell to Replace netdom Commands to Join the Domain. It turned out to be a very popular post. To [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/10961","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=10961"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/10961\/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=10961"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=10961"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=10961"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}