{"id":1413,"date":"2018-05-15T10:34:29","date_gmt":"2018-05-15T10:34:29","guid":{"rendered":"http:\/\/wiki.thomasandsofia.com\/?p=1413"},"modified":"2018-05-16T11:00:47","modified_gmt":"2018-05-16T11:00:47","slug":"powershell-scripting-for-windows-server-administrators","status":"publish","type":"post","link":"https:\/\/wiki.thomasandsofia.com\/?p=1413","title":{"rendered":"PowerShell Scripting for Windows Server Administrators"},"content":{"rendered":"<p><a href=\"http:\/\/wiki.thomasandsofia.com\/2018\/04\/18\/microsoft-windows-server-2016\/\">Main Menu<\/a><\/p>\n<h2>Resources<\/h2>\n<h3>PowerShell New-ADUser Variables<\/h3>\n<p><a href=\"https:\/\/docs.microsoft.com\/en-us\/previous-versions\/windows\/it-pro\/windows-server-2008-R2-and-2008\/ee617253(v=technet.10)\" target=\"_blank\" rel=\"noopener\">https:\/\/docs.microsoft.com\/en-us\/previous-versions\/windows\/it-pro\/windows-server-2008-R2-and-2008\/ee617253(v=technet.10)<\/a><\/p>\n<h3>Prerequisites<\/h3>\n<ul>\n<li>Logged into a DC or<\/li>\n<li>Have rset intsalled<\/li>\n<\/ul>\n<h1>Storing User Input Into Variables with PowerShell<\/h1>\n<p>36: <a href=\"https:\/\/www.udemy.com\/windows-server-2016\/learn\/v4\/t\/lecture\/6744866?start=0\" target=\"_blank\" rel=\"noopener\">https:\/\/www.udemy.com\/windows-server-2016\/learn\/v4\/t\/lecture\/6744866?start=0<\/a><\/p>\n<ul>\n<li>Open PowerShell ISE\n<ul>\n<li>Server Manager &gt; Tools &gt; Windows PowerShell ISE<\/li>\n<\/ul>\n<\/li>\n<li>PowerShell &gt; Script drop down (upper right corner next to &#8216;Script&#8217; to show the editor<\/li>\n<\/ul>\n<ol>\n<li># Store users name and password into variables<\/li>\n<li>$firstname = Read-Host -Prompt &#8220;Enter the user&#8217;s first name&#8221;<\/li>\n<li>$lastname = Read-Host -Prompt &#8220;Enter the user&#8217;s last name&#8221;<\/li>\n<li>$firstname = Read-Host -Prompt &#8220;Enter $firstname $lastname&#8217;s password&#8221;<\/li>\n<li># Display the results<\/li>\n<li>echo &#8220;$firstname $lastname&#8217;s password is $password.&#8221;<\/li>\n<\/ol>\n<ul>\n<li>Run the script by clicking the green &#8216;Play&#8217; button as shown below<\/li>\n<\/ul>\n<p><a href=\"http:\/\/wiki.thomasandsofia.com\/wp-content\/uploads\/2018\/05\/script.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1407\" src=\"http:\/\/wiki.thomasandsofia.com\/wp-content\/uploads\/2018\/05\/script.png\" alt=\"\" width=\"683\" height=\"295\" srcset=\"https:\/\/wiki.thomasandsofia.com\/wp-content\/uploads\/2018\/05\/script.png 683w, https:\/\/wiki.thomasandsofia.com\/wp-content\/uploads\/2018\/05\/script-300x130.png 300w, https:\/\/wiki.thomasandsofia.com\/wp-content\/uploads\/2018\/05\/script-150x65.png 150w\" sizes=\"auto, (max-width: 683px) 100vw, 683px\" \/><\/a><\/p>\n<p><a href=\"http:\/\/wiki.thomasandsofia.com\/wp-content\/uploads\/2018\/05\/script2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1408\" src=\"http:\/\/wiki.thomasandsofia.com\/wp-content\/uploads\/2018\/05\/script2.png\" alt=\"\" width=\"683\" height=\"296\" srcset=\"https:\/\/wiki.thomasandsofia.com\/wp-content\/uploads\/2018\/05\/script2.png 683w, https:\/\/wiki.thomasandsofia.com\/wp-content\/uploads\/2018\/05\/script2-300x130.png 300w, https:\/\/wiki.thomasandsofia.com\/wp-content\/uploads\/2018\/05\/script2-150x65.png 150w\" sizes=\"auto, (max-width: 683px) 100vw, 683px\" \/><\/a><\/p>\n<p>EASY!<\/p>\n<h1>Creating Active Directory User Accounts with PowerShell Part 1<\/h1>\n<p>37: <a href=\"https:\/\/www.udemy.com\/windows-server-2016\/learn\/v4\/t\/lecture\/7045012?start=0\" target=\"_blank\" rel=\"noopener\">https:\/\/www.udemy.com\/windows-server-2016\/learn\/v4\/t\/lecture\/7045012?start=0<\/a><\/p>\n<h3>Import the ActiveDirectory module.<\/h3>\n<ul>\n<li>If not on a Domain Controller or do not have rsat installed, your script will fail.<\/li>\n<\/ul>\n<pre># Import required modules\r\nImport-Module ActiveDirectory\r\n<\/pre>\n<h3>Find the path and store it in a variable<\/h3>\n<ul>\n<li>ServerManager &gt; Tools &gt; Active Directory Users and Computers<\/li>\n<li>View &gt; Advanced Features<\/li>\n<li>Rclick the OU you wish to use &gt; Properties<\/li>\n<li>Click the &#8216;Attribute Editor&#8217; tab<\/li>\n<li>Double click &#8216;distinguishedName&#8217; to access the value and copy it.<\/li>\n<\/ul>\n<pre># Specify where to store the user account\r\n$OUpath = \"OU=Domain Users,OU=thomasroberts.name,DC=thomasroberts,DC=name\"\r\n<\/pre>\n<h3>Convert the password into a secure string<\/h3>\n<pre># Convert the password to a secure string\r\n$securePassword = ConvertTo-SecureString $password -AsPlainText -Force\r\n<\/pre>\n<h3>Create the User Account<\/h3>\n<pre># Create the user account\r\nNew-ADUser -Name \"$firstname $lastname\" -GivenName $firstname -Surname $lastname -UserPrincipalName \"$firstname.$lastname\" -Path $OUpath -AccountPassword $securePassword -ChangePasswordAtLogon $True -Enabled $True\r\n<\/pre>\n<h3>Full Script to create a user<\/h3>\n<pre># Import Modules\r\nImport-Module ActiveDirectory\r\n\r\n# Store users name and password into variables\r\n$firstname = Read-Host -Prompt \"Enter the user's first name\"\r\n$lastname = Read-Host -Prompt \"Enter the user's last name\"\r\n$password = Read-Host -Prompt \"Enter $firstname $lastname's password\"\r\n\r\n# Display the results\r\necho \"$firstname $lastname's password is $password.\"\r\n\r\n# Specify where to store the user account\r\n$OUpath = \"OU=Domain Users,OU=thomasroberts.name,DC=thomasroberts,DC=name\"\r\n\r\n# Convert the password to a secure string\r\n$securePassword = ConvertTo-SecureString $password -AsPlainText -Force\r\n\r\n# Create the user account\r\nNew-ADUser -Name \"$firstname $lastname\" -GivenName $firstname -Surname $lastname -UserPrincipalName \"$firstname.$lastname\" -Path $OUpath -AccountPassword $securePassword -ChangePasswordAtLogon $True -Enabled $True\r\n<\/pre>\n<h1>Creating Active Directory User Accounts with PowerShell Part 2<\/h1>\n<p>38: <a href=\"https:\/\/www.udemy.com\/windows-server-2016\/learn\/v4\/t\/lecture\/7044978?start=0\" target=\"_blank\" rel=\"noopener\">https:\/\/www.udemy.com\/windows-server-2016\/learn\/v4\/t\/lecture\/7044978?start=0<\/a><\/p>\n<p>Add a While loop to add multiple users<\/p>\n<pre>$exit=\"\"\r\nwhile ($exit -ne 'q'){\r\n   ...\r\n   $exit = Read-Host -Prompt \"Enter more users? Enter 'q' to quit.\"\r\n}<\/pre>\n<h3>Saving a PowerShell Script<\/h3>\n<ul>\n<li>Save to your desktop or where ever<\/li>\n<li>Locate the script and Rclick &#8216;Run with PowerShell&#8221;\n<ul>\n<li>Execution Policy Change<\/li>\n<li>The execution policy helps protect you from scripts that you do not trust. Changing the execution policy might expose you to the security risks described in the about_Execution_Policies help topc at http:\/\/blah.blah. Do you want to change the execution policy?<\/li>\n<li>[Y} Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is &#8220;N&#8221;):<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h1>Creating Users Accounts from a CSV Spreadsheet with Powershell<\/h1>\n<p>39: <a href=\"https:\/\/www.udemy.com\/windows-server-2016\/learn\/v4\/t\/lecture\/7069922?start=0\" target=\"_blank\" rel=\"noopener\">https:\/\/www.udemy.com\/windows-server-2016\/learn\/v4\/t\/lecture\/7069922?start=0<\/a><\/p>\n<pre># Import Modules\r\nImport-Module ActiveDirectory\r\n\r\n# Get file path and read\r\n$filepath = Read-Host -Prompt \"Please enter the CSV file path\"\r\n$users = ImportCsv $filepath\r\n\r\n$securePassword = ConvertTo-SecureString \"TempP@ssw0rd\" -AsPlainText -Force\r\n\r\n# Loop through the users\r\nForEach ($user in $users)\r\n  # First line = column names\r\n  # Note: if 'First Name' is stored at 'FirstName', no quotes required!\r\n  $fname = $user.'First Name'\r\n  $lname = $user.'Last Name'\r\n  $jtitle = $user.'Job Title'\r\n  $officephone = $user.'Office Phone'\r\n  $emailaddress = $user.'Email Address'\r\n  $description = $user.Description\r\n  $OUpath = $user.'Organizational Unit'\r\n\r\n  # Create the user account\r\n  New-ADUser -Name \"$fname $lname\" -GivenName $fname -Surname $lname -UserPrincipalName \"$fname.$lname\" -Path $OUpath -AccountPassword $securePassword -ChangePasswordAtLogon $True -Enabled $True -OfficePhone $officephone -Description $description -EmailAddress $emailaddress\r\n\r\n  # Display the results\r\n  echo \"User $fname $lame has been created.\"\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h1>Expanding our Script for Users with Duplicate Names<\/p>\n<h1>\n<pre># Import Modules\r\nImport-Module ActiveDirectory\r\n\r\n# Get file path and read\r\n$filepath = Read-Host -Prompt \"Please enter the CSV file path\"\r\n$users = ImportCsv $filepath\r\n\r\n$securePassword = ConvertTo-SecureString \"TempP@ssw0rd\" -AsPlainText -Force\r\n\r\n# Loop through the users\r\nForEach ($user in $users)\r\n  # Do this for each user\r\n  $acctNumber=verifyUsername($user.'First Name'+\" \"+$user.'Last Name')\r\n  $username=($user.'First Name'[0]+\" \"+$user.'Last Name' + $acctNumber\r\n\r\n  New-ADUser `\r\n    -Name ($user.'First Name'+\" \"+$user.'Last Name' + \" \" + $acctNumber) `\r\n    -GivenName $user.'First Name' `\r\n    -Surname $user.'Last Name' `\r\n    -UserPrincipalName $username `\r\n    -SamAccountName $username `\r\n    -AccountPassword (ConvertTO-SecureString \"P@$$w0rd123\") `\r\n    -Description $user.Description `\r\n    -EmailAddress $user.\"Email Address\" `\r\n    -Title $user.\"Job Title\" `\r\n    -OfficePhone $user.\"Office Phone\" `\r\n    -Path $user.\"Organizational Unit\" `\r\n    -ChangePasswordAtLogon 1 `\r\n    -Enabled ([System.Convert]::ToBoolian($user.Enabled))\r\n}\r\n\r\nfunction verifyUsername ($username) {\r\n  $i=1\r\n  if (usernameTaken($username) -eq $True) {\r\n    while (usernameTaken($username))\r\n      $i++\r\n    }\r\n  } else {\r\n    return \"\"\r\n  }\r\n  return $i\r\n}\r\n\r\nfunction usernameTaken ($username) {\r\n  $test1 = Get-ADUser -Filter { userPrinicpalName -eq $username }\r\n  $test2 = Get-ADUser -Filter { SamAccountName -eq $username }\r\n\r\n  If ($test1 -eq $Null -and $test2 -eq $Null){\r\n    return $False\r\n  } else {\r\n    return $True\r\n  }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Main Menu Resources PowerShell New-ADUser Variables https:\/\/docs.microsoft.com\/en-us\/previous-versions\/windows\/it-pro\/windows-server-2008-R2-and-2008\/ee617253(v=technet.10) Prerequisites Logged into a DC or Have rset intsalled Storing User Input Into Variables with PowerShell 36: https:\/\/www.udemy.com\/windows-server-2016\/learn\/v4\/t\/lecture\/6744866?start=0 Open PowerShell ISE Server Manager &gt; Tools &gt; Windows PowerShell ISE PowerShell &gt; Script drop down (upper right corner next to &#8216;Script&#8217; to show the editor # Store users name ..<\/p>\n<div class=\"clear-fix\"><\/div>\n<p><a href=\"https:\/\/wiki.thomasandsofia.com\/?p=1413\" 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":[37],"tags":[],"class_list":["post-1413","post","type-post","status-publish","format-standard","hentry","category-microsoft-windows-server-2016"],"_links":{"self":[{"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/posts\/1413","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=1413"}],"version-history":[{"count":5,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/posts\/1413\/revisions"}],"predecessor-version":[{"id":1419,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/posts\/1413\/revisions\/1419"}],"wp:attachment":[{"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1413"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1413"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1413"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}