PC_Aide

0_New-OU.ps1

Jan 6th, 2017
378
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #-----------------------------------------------------------------------
  2. # FileName    : New-OU.ps1
  3. # Description : Powershell script creates OU into Active Directory from a CSV File.
  4. # Remarks     : CSV file must contains Name and Path.
  5. #              
  6. #-----------------------------------------------------------------------
  7. param([parameter(Mandatory=$true)] [String]$FileCSV)
  8. $listOU=Import-CSV $FileCSV -Delimiter ";"
  9.  
  10. ForEach($OU in $listOU){
  11.  
  12.     try{
  13.     #Get Name and Path from the source file
  14.     $OUName = $OU.Name
  15.     $OUPath = $OU.Path
  16.  
  17.     #Display the name and path of the new OU
  18.     Write-Host -Foregroundcolor Yellow $OUName $OUPath
  19.    
  20.     #Create OU
  21.     New-ADOrganizationalUnit -Name "$OUName" -Path "$OUPath" -ProtectedFromAccidentalDeletion $false
  22.    
  23.     #Display confirmation
  24.     Write-Host -ForegroundColor Green "OU $OUName created"
  25.     }catch{
  26.    
  27.         Write-Host $error[0].Exception.Message
  28.     }
  29.  
  30. }
Advertisement
Add Comment
Please, Sign In to add comment