Basic PowerShell Script

Greetings People,

I am very new to powershell and basically i want to run a CSV script that creates an Organizational Unit and a group within that Organizational Unit with the same name. I am not sure how to go about this, do I have to put the powershell commands into a CSV file and just Import-CSV -Path “Path” because doing this does not work.

Acheiveable, can you share the code you have, it will help others to understand the problem in the code better

Well, the powershell commands would go in a .ps1 file.

You have to show your work for us to try and help.

What you are asking for help on is done every day.

It’s OK to be new to anything. We all have to start somewhere. Yet, you really should spend time getting to know a tool before using it. Especially in production environments, as you could really cause major damage. There are plenty of free / no cost or low cost online videos, courses, book, etc., you can use.

Windows PowerShell Survival Guide
The purpose of this document is to help you to learn more about PowerShell and to be successful in applying it. This document seeks to point to the best content on the web to enable you to reach that goal.
https://social.technet.microsoft.com/wiki/contents/articles/183.windows-powershell-survival-guide.aspx

See also:
https://www.reddit.com/r/PowerShell/comments/99dc5d/powershell_for_a_noob

Read the built-in help files fully

# Get parameters, examples, full and Online help for a cmdlet or function

(Get-Command -Name Get-Content).Parameters
Get-help -Name Get-Content -Examples
Get-help -Name Get-Content -Full
Get-help -Name Get-Content -Online

Get-Help about_*
Get-Help about_Functions

There are tools that will write the Active Directory code for you, already on Windows server.

Active Directory Administrative Center: Getting Started
https://technet.microsoft.com/en-us/library/dd560651(v=ws.10).aspx

Active Directory Administrative Center
https://docs.microsoft.com/en-us/windows-server/identity/ad-ds/get-started/adac/active-directory-administrative-center

Step-By-Step: Utilizing PowerShell History Viewer in Windows Server 2012 R2
https://blogs.technet.microsoft.com/canitpro/2015/03/04/step-by-step-utilizing-powershell-history-viewer-in-windows-server-2012-r2

Reading in a txt / csv file is an intro to PowerShell thing, and done daily by anyone using it. Again, there are tons for resources all over the web showing exactly how to do this. For example, though this is talking about creating users, creating any AD object is the same, for the most part.

Use PowerShell to Read a CSV file and Create Active Directory User Accounts
https://devblogs.microsoft.com/scripting/use-powershell-to-read-a-csv-file-and-create-active-directory-user-accounts

There are lots of videos on YouTube, Microsoft Virtual Academy, MS Channel9 on PowerShell holistically and some on this very topic, meaning creating AD objects using PowerShell.

https://mva.microsoft.com/en-us/training-courses/getting-started-with-microsoft-powershell-8276?l=r54IrOWy_2304984382
https://mva.microsoft.com/search/SearchResults.aspx#!q=powershell&lang=1033
https://www.youtube.com/results?search_query=beginning+powershell

There are pre-built scripts in the MS powershelgallery.com for the same.

https://www.powershellgallery.com

Bulk import of organizational units from CSV
I had a project where i needed to replicate the OU structure in a dummy test domain for validation of group policy objects (things like mapped drives/printers for groups) I created this script to import the OU structure that I had pulled from our existing AD environment into my
https://gallery.technet.microsoft.com/scriptcenter/Bulk-import-of-organization-82cb9ebe

Yet without learning PowerShell, you may not fully understand what and why something is the why it is or works he way it does.

Rule #1 is never run any code, form anyone, that you don’t fully understand the outcomes / impacts of running it. Never do it in a production environment, if you have not done it in a test lab first. If you don’t have a test lab, then use the MS TechNet Virtual Labs to practice.

TechNet Windows Server 2012 Virtual Lab
https://blogs.technet.microsoft.com/schadinio/2012/07/10/technet-windows-server-2012-virtual-labs

Reference books you should thoroughly read and reference regularly in your efforts.

Beginning —
Learn Windows PowerShell in a Month of Lunches 3rd Edition
Donald W. Jones (Author),‎ Jeffrey Hicks (Author)
ISBN-13: 978-1617294167
ISBN-10: 1617294160

Internediate —
Windows PowerShell Cookbook: The Complete Guide to Scripting Microsoft’s Command Shell 3rd Edition
Lee Holmes (Author)
ISBN-13: 978-1449320683
ISBN-10: 1449320686

Advanced —
Windows PowerShell in Action 3rd Edition
by Bruce Payette (Author),‎ Richard Siddaway (Author)
ISBN-13: 978-1633430297
ISBN-10: 1633430294

eBooks…

https://blogs.technet.microsoft.com/pstips/2014/05/26/free-powershell-ebooks
https://www.idera.com/resourcecentral/whitepapers/powershell-ebook
https://powershell.org/ebooks

Courses
MOC on-demand, if you cannot go in person.
https://www.microsoftondemand.com/courses/microsoft-course-10961
https://www.microsoftondemand.com/courses/microsoft-course-10962

Wow, that’s a lot of resources, nice!

And if you find yourself tripping over the syntax a bit much, I highly recommend PSKoans as a nice crash course. :smiley:

This is basically what ive come up with from tutorials and such but all this information is from an outside CSV file. But is there a way to have 1 CSV file to be imported into powershell that can create the OU and the group in the active directory

# Import active directory module for running AD cmdlets
Import-Module activedirectory

#Store the data from ADUsers.csv in the $ADUsers variable
$ADUsers = Import-csv .\users1.csv

#Loop through each row containing user details in the CSV file
foreach ($User in $ADUsers)
{
#Read user data from each field in each row and assign the data to a variable as below

$Firstname = $User.firstname
$Lastname = $User.lastname
$Username = $User.username
$email = $User.email
$Password = $User.password
$OUName = $User.ouname
$BaseDN = $User.basedn
$Group = $User.group
#This field refers to the OU the user account is to be created in
$OU = "OU=" + $OUName + "," + $BaseDN

#Create New Organizational Unit

if(Get-ADOrganizationalUnit -Filter "distinguishedName -eq '$OU'") {
#Write-Host "$OU already exists."
}else {
New-ADOrganizationalUnit -Name $OUName -Path $BaseDN
Write-Host "Sucessfully Created $OUName OU"
}

#Create New Group

$GroupExists = Get-ADGroup -LDAPFilter "(SAMAccountName=$Group)"
if ($GroupExists -eq $null){
New-ADGroup -Name $Group -GroupCategory Security -GroupScope Global -Path $OU
Write-Host "Sucessfully Created $Group Group"
}

#Check to see if the user already exists in AD

if (Get-ADUser -F {SamAccountName -eq $Username}){

#If user does exist, add to group
Add-ADGroupMember -Identity $Group -Members $Username
Write-Host "Sucessfully Added $Username to $Group Group"
}
else{

#User does not exist then proceed to create the new user account and add to group
New-ADUser `
-SamAccountName $Username `
-UserPrincipalName "$Username@misfits.org" `
-Name "$Firstname $Lastname" `
-GivenName $Firstname `
-Surname $Lastname `
-Enabled $True `
-DisplayName "$Lastname, $Firstname" `
-Path $OU `
-EmailAddress $email `
-AccountPassword (convertto-securestring $Password -AsPlainText -Force) -ChangePasswordAtLogon $True

Write-Host "Sucessfully Created $Username User"

Add-ADGroupMember -Identity $Group -Members $Username

Write-Host "Sucessfully Added $Username to $Group Group"

}

}

This is basically what I’ve come up with after all the tutorials and such ive been researching.

@postanote thank you for all of these resources! I will take a look at them!