Basic PowerShell Script

Welcome Forums General PowerShell Q&A Basic PowerShell Script

This topic contains 7 replies, has 5 voices, and was last updated by

2 months, 1 week ago.

  • Author
  • #151580

    Topics: 1
    Replies: 3
    Points: 0
    Rank: Member

    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.

  • #151589

    Topics: 8
    Replies: 891
    Points: 2,749
    Helping Hand
    Rank: Community Hero

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

  • #151605

    Topics: 22
    Replies: 626
    Points: 1,317
    Helping Hand
    Rank: Community Hero

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

  • #151614

    Topics: 2
    Replies: 968
    Points: 1,784
    Helping Hand
    Rank: Community Hero

    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.

    See also:

    Powershell for a Noob from PowerShell

    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

    Active Directory Administrative Center

    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

    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.!q=powershell&lang=1033

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

    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

    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

    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


    MOC on-demand, if you cannot go in person.

    Microsoft Course 10961

    Microsoft Course 10962

    • #151650

      Topics: 1
      Replies: 3
      Points: 0
      Rank: Member

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

  • #151626

    Topics: 2
    Replies: 442
    Points: 924
    Helping Hand
    Rank: Major Contributor

    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. 😀

  • #151643

    Topics: 1
    Replies: 3
    Points: 0
    Rank: Member

    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 = $
    $Password = $User.password
    $OUName = $User.ouname
    $BaseDN = $User.basedn
    $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"
    #User does not exist then proceed to create the new user account and add to group
    New-ADUser `
    -SamAccountName $Username `
    -UserPrincipalName "$" `
    -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"
  • #151647

    Topics: 1
    Replies: 3
    Points: 0
    Rank: Member

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

You must be logged in to reply to this topic.