Category Archives: Powershell

Enable multiple Active Directory users for Lync/Skype for Business through management shell


User management for any enterprise business class application is a key. This kind of task comes under administration but some time implementation team also perform these activities. This post provides a little help to the implementation consultant or UC administrator.  To enable group of users for Lync/Skype for Business is very common but through control panel you can enable maximum 5000 users at a time and you may have to try the same thing multiple time if any error occurs.  If we try the same thing through Skype for Business server management shell, we can easily perform the task in better way and there is no limit 🙂

If you want enable multiple users with a specific commonality between them, you can use LDAPFilter to the same.

For example: I have common name between all the users. You can have common “Department”, “OU” or something else which can be used to filter the users.

Get-CsAduser -LDAPFilter “(name=sfb*)” | Enable-CsUser -RegistrarPool “poolfqdn” -SipAddressType EmailAddress

This is just an example, you can do even much better scripting using LDAPFilter.

 

Advertisement

Get group membership of AD user


To get single user membership:

Get-ADPrincipalGroupMembership -Identity “username” | select name | ft

To get multiple user membership:

Import-Csv “C:\users.csv” | ForEach { $group = (Get-ADPrincipalGroupMembership -Identity $_.username).Name; New-Object -type PSObject @{$_.username = group}} | Out-File -FilePath “c:\result.csv”

#users.csv is a file for list of users.

Username

user1

user2

user3

Remove all secondary email addresses through powershell script


$Input = Get-Content C:\users.txt | Get-Mailbox
$Input | foreach {
write-host “$($_.Name) —- $($_.EmailAddresses.Count)”
for ($i = $_.EmailAddresses.Count ;$i -ge 0; $i — )
{
$_.EmailAddresses[$i].ProxyAddressString  
if ($_.EmailAddresses[$i].IsPrimaryAddress  -eq $false )
{
$_.EmailAddresses.RemoveAt($i)

}
  set-mailbox  $_.Identity -EmailAddresses $_.EmailAdresses
}

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:

Alias

Name

UPN

EmailAddress

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.

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 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:

   

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.

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.

Create bulk users in Active Directory 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:

Change UPN through Powershell Scriptl


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.