{"id":3978,"date":"2024-05-13T22:57:20","date_gmt":"2024-05-13T22:57:20","guid":{"rendered":"https:\/\/wiki.thomasandsofia.com\/?p=3978"},"modified":"2024-05-13T23:07:22","modified_gmt":"2024-05-13T23:07:22","slug":"powershell-working-with-active-director","status":"publish","type":"post","link":"https:\/\/wiki.thomasandsofia.com\/?p=3978","title":{"rendered":"Powershell: Working with Active Director"},"content":{"rendered":"<h1>Using Powershell with Active Directory<\/h1>\n<p><strong><em>Hint: Enable View &gt; Advanced Features in the AD GUI to view all the properties a user has.<\/em><\/strong><\/p>\n<p><strong>Import the AD Module<\/strong><\/p>\n<pre>Import-Module ActiveDirectory<\/pre>\n<p>Add a new AD User<\/p>\n<pre>#New-ADUser -Name \"&lt;Full Name&gt;\" -GivenName \"&lt;Firstname&gt;\" -Surname \"&lt;Lastname&gt;\" -SamAccountName \"&lt;shortname&gt;\" -UserPrincipalName \"&lt;email@address.tld&gt;\" -Path \"OU=Administration,OU=Staff,OU=STARWARS,DC=starwars,DC=com\" -AccountPassword(ConvertTo-SecureString \"&lt;Password&gt;\" -AsPlainText -force) -Enabled $true\r\nNew-ADUser -Name \"Luke Skywalker\" -GivenName \"Luke\" -Surname \"Skywalker\" -SamAccountName \"lskywalker\" -UserPrincipalName \"lskywalker@starwars.com\" ...<\/pre>\n<p><strong>Get an AD User<\/strong><\/p>\n<p>Get-ADUser &lt;username&gt;<\/p>\n<p>This returns the user data as an Object.<\/p>\n<p><strong>Assign the user to a variable<\/strong><\/p>\n<pre>$user = Get-ADUser &lt;username&gt;<\/pre>\n<p>Now you can manipulate or display what ever data you need.<\/p>\n<p><em>Note: When running from the Powershell cli, you can press &lt;Tab&gt; after the &#8216;.&#8217; to scroll through the available properties.<\/em><\/p>\n<pre>Write-Host $user.GivenName<\/pre>\n<h3>Setting User Properties<\/h3>\n<p><strong>Set-ADUser<\/strong><\/p>\n<pre>#Set-ADUser -Identity &lt;SamAccountName&gt; -&lt;property&gt; &lt;NewValue&gt;\r\nSet-ADUser -Identity fbaggins -Surname Roberts<\/pre>\n<h4>Resetting a Password<\/h4>\n<pre>Set-ADAccountPassword -Identity &lt;SamAccountName&gt; -Reset -NewPassword(ConvertTo-SecureString -AsPlainText \"&lt;NewSecurePassword&gt;\" -Force)<\/pre>\n<h3>Managing Groups<\/h3>\n<h4>Adding a User to a Group<\/h4>\n<p><strong>Add-ADGroupMember<\/strong><\/p>\n<pre>#Add-ADGroupMember -Identity &lt;GroupName&gt; -Members &lt;SamAccountName&gt;\r\nAdd-ADGroupMember -Identity Fellowship -Members fbaggins<\/pre>\n<h4>Removing a user from a group<\/h4>\n<p><strong>Remove-ADGroupMember<\/strong><\/p>\n<pre>#Remove-ADGroupMember -Identity &lt;GroupName&gt; -Members &lt;SamAccountName&gt;<\/pre>\n<p><em>Note: Will require confirmation &#8211; Are you sure?<\/em><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Using Powershell with Active Directory Hint: Enable View &gt; Advanced Features in the AD GUI to view all the properties a user has. Import the AD Module Import-Module ActiveDirectory Add a new AD User #New-ADUser -Name &#8220;&lt;Full Name&gt;&#8221; -GivenName &#8220;&lt;Firstname&gt;&#8221; -Surname &#8220;&lt;Lastname&gt;&#8221; -SamAccountName &#8220;&lt;shortname&gt;&#8221; -UserPrincipalName &#8220;&lt;email@address.tld&gt;&#8221; -Path &#8220;OU=Administration,OU=Staff,OU=STARWARS,DC=starwars,DC=com&#8221; -AccountPassword(ConvertTo-SecureString &#8220;&lt;Password&gt;&#8221; -AsPlainText -force) -Enabled $true New-ADUser ..<\/p>\n<div class=\"clear-fix\"><\/div>\n<p><a href=\"https:\/\/wiki.thomasandsofia.com\/?p=3978\" title=\"read more...\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[33,82,83],"tags":[],"class_list":["post-3978","post","type-post","status-publish","format-standard","hentry","category-active-directory","category-powershell","category-programming"],"_links":{"self":[{"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/posts\/3978","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3978"}],"version-history":[{"count":5,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/posts\/3978\/revisions"}],"predecessor-version":[{"id":3983,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/posts\/3978\/revisions\/3983"}],"wp:attachment":[{"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3978"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3978"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3978"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}