Wednesday, January 30, 2013

Create Bulk users in AD through Powershell

# Script:

Import-Module ActiveDirectory

Import-Csv .\Users.csv | ForEach {New-ADUser -SamAccountName $_.SamAccountName -UserPrincipalName $_.UPN -GivenName $_.FirstName -Name $_.Name -DisplayName $_.DisplayName -Surname $_.LastName -Department $_.Department -AccountPassword (ConvertTo-SecureString "Password@123" -AsPlainText -Force) -Enabled $true -ChangePasswordAtLogon $true -PassThru -Path $_.Path}


# CSV File Format:

FirstName
Name
LastName
DisplayName
SamAccountName
UPN
Path
Department
Ajay
Ajay Kakkar
Kakkar
Ajay Kakkar
ajayk
ajayk@WIN2K8.COM
OU=Test,DC=WIN2K8,DC=COM
IT

Sunday, January 20, 2013

Change UPN through Powershell Script

Import-Module ActiveDirectory

#Change old suffix according to your environment
$oldSuffix = 'WIN2K8.COM'

#Change new suffix according to your environment
$newSuffix = 'ABC.COM'

#Change OU according to environment, you want to change suffixes for
$ou = "OU=Test,DC=WIN2K8,DC=COM"

#Change the name of your AD server according to your environment
$server = "dc08.win2k8.com"

Get-ADUser -SearchBase $ou -filter * | ForEach-Object {$newUpn = $_.UserPrincipalName.Replace($oldSuffix,$newSuffix)
$_ | Set-ADUser -server $server -UserPrincipalName $newUpn}

Note: Please make sure suffixes should be case sensitive according to your environment.

Saturday, January 19, 2013

AD DS Deployment through Windows Powershell - Windows Server 2012


#
# Windows PowerShell script for AD DS Deployment
#

Import-Module ADDSDeployment
Install-ADDSForest `
-CreateDnsDelegation:$false `
-DatabasePath "C:\Windows\NTDS" `
-DomainMode "Win2012" `
-DomainName "WIN2K12.COM" `
-DomainNetbiosName "WIN2K12" `
-ForestMode "Win2012" `
-InstallDns:$true `
-LogPath "C:\Windows\NTDS" `
-NoRebootOnCompletion:$false `
-SysvolPath "C:\Windows\SYSVOL" `
-Force:$true

Note: This is a script which can be run for AD DS configuration to promote the first server as a DC in new forest. Please install prerequisite through Server Manager before running this script.

Friday, January 11, 2013

Enable Mailbox through Powershell for existing AD users

Script:
Import-CSV "C:\text.csv" | ForEach {Enable-Mailbox -Identity $_.UPN -Database “Mailbox Database 1” -Alias $_.alias | Set-Mailbox -EmailAddressPolicyEnabled $false -PrimarySmtpAddress $_.EmailAddress}

CSV File Format:
UPNAliasEmailAddress
Ajay@contoso.comAjaykAjay.kakkar@contoso.com


Alias: Alias name for the user(like:ajayk)
UPN: User logon name (like: ajayk@contoso.com)
Email Address: User email address(like: ajaykakkar@contoso.com)

Note: Alias and Email Address could be same or different as per your requirement.

Disk space report of remote computer/servers through powershell

Script:
Param (
$computers = (Get-Content  "C:\Scripts\Computers.txt")
)

$Title="Hard Drive Report to HTML"

#embed a stylesheet in the html header
$head = @"
<mce:style><!--
mce:0
--></mce:style><style _mce_bogus="1"><!--
mce:0
--></style>
<Title>$Title</Title>
<br>
"@ 

#define an array for html fragments
$fragments=@()

#get the drive data
$data=Get-WmiObject -Class Win32_logicaldisk -filter "drivetype=3" -computer $computers

#group data by computername
$groups=$Data | Group-Object -Property SystemName

#this is the graph character
[string]$g=[char]9608 

#create html fragments for each computer
#iterate through each group object
        
ForEach ($computer in $groups) {
    
    $fragments+="<H2>$($computer.Name)</H2>"
    
    #define a collection of drives from the group object
    $Drives=$computer.group
    
    #create an html fragment
    $html=$drives | Select @{Name="Drive";Expression={$_.DeviceID}},
    @{Name="SizeGB";Expression={$_.Size/1GB  -as [int]}},
    @{Name="UsedGB";Expression={"{0:N2}" -f (($_.Size - $_.Freespace)/1GB) }},
    @{Name="FreeGB";Expression={"{0:N2}" -f ($_.FreeSpace/1GB) }},
    @{Name="Usage";Expression={
      $UsedPer= (($_.Size - $_.Freespace)/$_.Size)*100
      $UsedGraph=$g * ($UsedPer/2)
      $FreeGraph=$g* ((100-$UsedPer)/2)
      #I'm using place holders for the < and > characters
      "xopenFont color=Redxclose{0}xopen/FontxclosexopenFont Color=Greenxclose{1}xopen/fontxclose" -f $usedGraph,$FreeGraph
    }} | ConvertTo-Html -Fragment 
    
    #replace the tag place holders. It is a hack but it works.
    $html=$html -replace "xopen","<"
    $html=$html -replace "xclose",">"
    
    #add to fragments
    $Fragments+=$html
    
    #insert a return between each computer
    $fragments+="<br>"
    
} #foreach computer

#add a footer
$footer=("<br><I>Report run {0} by {1}\{2}<I>" -f (Get-Date -displayhint date),$env:userdomain,$env:username)
$fragments+=$footer

#write the result to a file
ConvertTo-Html -head $head -body $fragments | Out-File c:\Report.htm

**********************************************************************************
Note:
#Require Powershell version 2.0

Enable Mailboxes for existing AD accounts through Powershell

Script:
Import-CSV "C:\text.csv" | ForEach {Enable-Mailbox -Identity $_.UPN -Database “Mailbox Database 1” -Alias $_.alias | Set-Mailbox -EmailAddressPolicyEnabled $false -PrimarySmtpAddress $_.EmailAddress}

CSV File Format:
UPNAliasEmailAddress
Ajay@contoso.comAjaykAjay.kakkar@contoso.com


Alias: Alias name for the user(like:ajayk)
UPN: User logon name (like: ajayk@contoso.com)
Email Address: User email address(like: ajaykakkar@contoso.com)

Note: Alias and Email Address could be same or different as per your requirement.

Create Mailboxes with new AD account through Powershell

Script:
$Password=Read-Host “Enter Password” -AsSecureString
Import-CSV "C:\users.csv" | ForEach {New-Mailbox -Alias $_.alias -Name $_.name -userPrincipalName $_.UPN -Database “Mailbox Database 1” -OrganizationalUnit EmailUsers -Password $Password |Set-mailbox -EmailAddressPolicyEnabled $false -PrimarySmtpAddress $_.EmailAddress}

CSV File Format:
AliasNameUPNEmailAddress

Alias: Alias name for the user(like:ajay.kakkar)
Name: Full user name(like: Ajay Kakkar)
UPN: User logon name(like: ajayk@contoso.com)
Email Address: User email address(like: ajaykakkar@contoso.com)

Note: Alias, UPN and Email Address could be same or different as per your requirement.