{"id":959,"date":"2014-07-23T00:01:00","date_gmt":"2014-07-23T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2014\/07\/23\/transforming-the-active-directory-cmdlets-part-3\/"},"modified":"2014-07-23T00:01:00","modified_gmt":"2014-07-23T00:01:00","slug":"transforming-the-active-directory-cmdlets-part-3","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/transforming-the-active-directory-cmdlets-part-3\/","title":{"rendered":"Transforming the Active Directory Cmdlets: Part 3"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Learn how to translate between ADSI, Quest, and Windows PowerShell cmdlets for creating users.<\/span>\n<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!\nI downloaded a script from the Internet to remove users, but it&#8217;s designed to use the newer Windows PowerShell cmdlets. My server environment doesn&#8217;t have them available yet. Can you help me convert to them?\n&mdash;RD\n<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 RD,\nHonorary Scripting Guy, Sean Kearney, here. I&#8217;m working with my old friend, Kevin. This is the third post in a series. For more, see:<\/p>\n<ul>\n<li><a href=\"http:\/\/blogs.technet.comhttps:\/\/devblogs.microsoft.com\/scripting\/transforming-active-directory-cmdlets-part-1\/\" target=\"_blank\">Transforming to<b> <\/b>Active Directory Cmdlets: Part 1<\/a><\/li>\n<li><a href=\"http:\/\/blogs.technet.comhttps:\/\/devblogs.microsoft.com\/scripting\/transforming-active-directory-cmdlets-part-2\/\" target=\"_blank\">Transforming to<b> <\/b>Active Directory Cmdlets: Part 2<\/a><\/li>\n<\/ul>\n<p>Kevin discovered the power of Bing, and he found a really cool script for purging users. He found a snag though.\n&#8220;I have to do some work in a remote location, and they don&#8217;t have a newer domain controller there yet. The change request is still in process, and we need to purge a lot of users. I found a great script to do this, but it only works with the new Windows PowerShell cmdlets.&#8221;\nHe sat there with big yellow Labrador eyes blinking, &#8220;Can you help out a guy here?&#8221;\nThe script was actually not much more than the following. It provides an <b>Import-CSV<\/b> to pull in a list of user names, then a cmdlet to delete them:<\/p>\n<p style=\"margin-left:30px\">$PurgeList=IMPORT-CSV PurgeList.Csv<\/p>\n<p style=\"margin-left:30px\">Foreach ($User in $PurgeList)<\/p>\n<p style=\"margin-left:30px\">{<\/p>\n<p style=\"margin-left:30px\">Remove-ADuser $User.Name &ndash;confirm:$False<\/p>\n<p style=\"margin-left:30px\">}\nKevin shared some information from the instructions, &#8220;I give it a list of SamAccountNames in a CSV file and simply run it through. On the remote site, I&#8217;ve got the Quest cmdlets installed on a management workstation.&#8221;\n&#8220;Ah good. So you&#8217;re not going to accidentally reboot anymore file servers?&#8221; I nudged him in the side. Quickly I showed him the Quest cmdlet for removing a user:<\/p>\n<p style=\"margin-left:30px\">REMOVE-QADObject\n&#8220;Believe it or not, this one behaves almost identically to the Windows PowerShell cmdlet, but instead of using <b>&ndash;confirm<\/b>, we use <b>&ndash;force<\/b>:&#8221;<\/p>\n<p style=\"margin-left:30px\">$PurgeList=IMPORT-CSV PurgeList.Csv<\/p>\n<p style=\"margin-left:30px\">Foreach ($User in $PurgeList)<\/p>\n<p style=\"margin-left:30px\">{<\/p>\n<p style=\"margin-left:30px\">Remove-QADobject $User.Name &ndash;force<\/p>\n<p style=\"margin-left:30px\">}\nKevin&#8217;s jaw dropped to the floor. &#8220;That&#8217;s it? I thought there was going to be something cool and challenging about this&hellip;&#8221;\n&#8220;Well,&#8221; I smiled, &#8220;It would be if you were asking me to do this with the <b>[ADSI]<\/b> accelerator. There&#8217;s a little bit extra there.&#8221;\nKevin rubbed his hands with glee, &#8220;Ah! Fun stuff!&#8221;\n&#8220;Yep. We have to drop in a whole five extra lines after we remove the cmdlet!&#8221;<\/p>\n<p style=\"margin-left:30px\">$Name=&#8217;CN=&#8217;+$User.Name<\/p>\n<p style=\"margin-left:30px\">$Parent=&#8217;OU=Grok,DC=Contoso,DC=Local&#8217;<\/p>\n<p style=\"margin-left:30px\">$Class=&#8221;user&#8221;<\/p>\n<p style=\"margin-left:30px\">$Connection=[ADSI]&#8221;LDAP:\/\/$Parent&#8221;<\/p>\n<p style=\"margin-left:30px\">$DeleteUser=$Connection.delete($Class, $Name)\nSo the same script using<strong> [ADSI]<\/strong> would look like this:<\/p>\n<p style=\"margin-left:30px\">$PurgeList=IMPORT-CSV PurgeList.Csv<\/p>\n<p style=\"margin-left:30px\">Foreach ($User in $PurgeList)<\/p>\n<p style=\"margin-left:30px\">{<\/p>\n<p style=\"margin-left:30px\">$Name=&#8217;CN=&#8217;+$User.Name<\/p>\n<p style=\"margin-left:30px\">$Parent=&#8217;OU=Grok,DC=Contoso,DC=Local&#8217;<\/p>\n<p style=\"margin-left:30px\">$Class=&#8221;user&#8221;<\/p>\n<p style=\"margin-left:30px\">$Connection=[ADSI]&#8221;LDAP:\/\/$Parent&#8221;<\/p>\n<p style=\"margin-left:30px\">$DeleteUser=$Connection.delete($Class, $Name)<\/p>\n<p style=\"margin-left:30px\">}\n&#8220;Now if you want really hard and scary, we could sit down and this with VBScript.&#8221;\nI&#8217;ve never seen anybody turn around and run so fast.\nPop in tomorrow as Kevin and I have some more &#8220;transforming fun&#8221; with Active Directory and Windows PowerShell.\nI invite you to follow The Scripting Guys 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 an email to The Scripting Guys at <a href=\"http:\/\/blogs.technet.commailto: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 remember eat your cmdlets each and every day with a dash of creativity.\n<b>Sean Kearney, <\/b>Windows PowerShell MVP and Honorary Scripting Guy<span style=\"font-size:12px\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Learn how to translate between ADSI, Quest, and Windows PowerShell cmdlets for creating users. &nbsp;Hey, Scripting Guy! I downloaded a script from the Internet to remove users, but it&#8217;s designed to use the newer Windows PowerShell cmdlets. My server environment doesn&#8217;t have them available yet. Can you help me convert to them? &mdash;RD &nbsp;Hello [&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,56,3,154,45],"class_list":["post-959","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-active-directory","tag-guest-blogger","tag-scripting-guy","tag-sean-kearney","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Learn how to translate between ADSI, Quest, and Windows PowerShell cmdlets for creating users. &nbsp;Hey, Scripting Guy! I downloaded a script from the Internet to remove users, but it&#8217;s designed to use the newer Windows PowerShell cmdlets. My server environment doesn&#8217;t have them available yet. Can you help me convert to them? &mdash;RD &nbsp;Hello [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/959","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=959"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/959\/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=959"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=959"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=959"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}