Posts Tagged ‘powershell’

Check for Active Directory replication failures

A quick and easy way to check your Active Directory for any replication errors using Powershell.

Get-ADReplicationPartnerMetadata -Scope Domain -Target (Domain name) | ft Server,LastReplicationSuccess,ConsecutiveReplicationFailures -AutoSize

This will list all your Domain Controllers and when they last replicated successfully and show how many consecutive replications errors they have (if any).

Configure DHCP server with cmd or powershell

I wanted to share real quick how you set up and configure a DHCP server on Windows Server 2008 R2 or 2012 (R2) with he command line or powershell.

Windows Server 2008 R2:
Let’s begin with Windows Server 2008 R2, which doesn’t have any dhcp cmdlets in powershell so we have to mostly stick to the commandline.

To install the DHCP role, open an elevated powershell window:
Import-module ServerManager
Add-windowsfeature -Name DHCP

Note that the DHCP Server service is installed but stopped and disabled. Now we have to go to an elevated command prompt for the rest.To set the service to automatic startup and start the service.
sc config dhcpserver start= auto
net start dhcpserver

Authorize the server in AD:
netsh dhcp add server Server1.dhcplab.local

Create a scope, which is empty
netsh dhcp server add scope "Scope1"

Add a range of IP addresses for leasing
netsh dhcp server scope add iprange

Set the default gateway option
netsh dhcp server scope set optionvalue 003 IPADDRESS

Set the lease time in seconds
netsh dhcp server scope set optionvalue 51 DWORD 28800

Set the DNS server
netsh dhcp server scope set optionvalue 006 IPADDRESS

Add a DHCP reservation, for a printer in this case
netsh dhcp server scope add reservedip A1B2C3D4E5F6 "Printer1" "Reservation for Printer1"

Windows Server 2012
Everything in an elevated powershell window. How sweet it is! 🙂

Install the DHCP Role (no need to configure the service afterwards)
add-windowsfeature -Name DHCP -includemanagementtools

Authorize the DHCP server

Add scope with IP range for leasing
Add-DhcpServerv4Scope -Name "Scope2" -StartRange -EndRange -SubnetMask

Set lease duration (days.hours:minutes:seconds)
Set-DhcpServerv4Scope -ScopeId -LeaseDuration 1.00:00:00

Set default gateway
Set-DhcpServerv4OptionValue -ScopeId -Router

Set dns server and domain name
Set-DhcpServerv4OptionValue -DnsServer -DnsDomain dhcplab.local

Set IP reservation, again for a printer
Add-DhcpServerv4Reservation -ScopeId -IPAddress -ClientId A1-B2-C3-D4-E5-F6 -Description "Reservation for Printer1" -Name "Printer1"

Powershell: Exchange – List all mailboxes in one database

To list all mailboxes in one database and sort it by size, use this nice PowerShell line:

Get-MailboxStatistics -Database <DATABASENAME> | select DisplayName, ItemCount, TotalItemSize | Sort-Object TotalItemSize -Descending


If you want to export the list to an CSV file, put “| Export-CSV C:\MailBoxSize.CSV” at the end of the PowerShell line.

Powershell: Exchange – Check database size

Here is a one liner to check the database size for exchange databases.

Start Exchange management shell. Then run the following command:

Get-MailboxDatabase | foreach-object {add-member -inputobject $_ -membertype noteproperty -name mailboxdbsizeinGB -value ([math]::Round(([int64](get-wmiobject cim_datafile -computername $_.server -filter (‘name=”’ + $_.edbfilepath.pathname.replace(“\”,”\\”) + ””)).filesize / 1GB),2)) -passthru} | Sort-Object mailboxdbsizeinGB -Descending | format-table identity,mailboxdbsizeinGB


Exchange tips: Out of Office on users

To check information about Out Of Office for mailbox users we can run an powershell “one liner” and get useful information.

The PS, Get-MailboxAutoReplyConfiguration “username” will show you this useful information:

  • AutoReplyState (Disabled/Enabled)
  • Start date (date and time)
  • End date (date and time)
  • External Message (in HTML format)
  • Internal Message (in HTML format)


With this command you can easily change the message as wel!

Get-MailboxAutoReplyConfiguration julras | Set-MailboxAutoReplyConfiguration -ExternalMessage “Hi, I`m testing OOO” -InternalMessage “Hi, I`m testing OOO” -Autoreplystate Enabled

As you can see in my PS command i here GET the results of “julras” user and pipe it throug an SET command to change the External and internal message and then Enable the Out of Office for the user.

Exchange 2010 – Export mailbox to pst

December 30, 2013 Leave a comment

Start Exchange Management Shell.
Give your self rights to do export and import
New-ManagementRoleAssignment –Role “Mailbox Import Export” –User Administrator

Then close Exchange Management Shell and restart it.

Now export mailbox using this command:
New-MailboxExportRequest -mailbox -FilePath \\localhost\temp$\user.pst

The export command needs a UNC path for the filepath. I have just created a hidden temp share. Remember to delete temps after job is completed 🙂

Find disabled users with their group membership and remove them from their groups

To quickly see the disabled users and their group membership in your Active Directory you can use this Powershell command:

Get-ADUser -SearchBase “OU=OU1,DC=domain,DC=local” -Filter ‘enabled -ne $True’ -Properties memberof | ft samaccountname, MemberOf -auto

This script will prompt you for a searchbase (Like “OU=OU1,DC=lab2,DC=local”) and remove all disabled users from their groups:

$inputfromuser = Read-Host ‘Enter AD Searchbase ‘
if ($inputfromuser -like “”)
Write-Host “Input error”

$Diableduser = Get-ADUser -SearchBase $inputfromuser -Filter ‘enabled -ne $True’ -Properties memberof
foreach ($user in $Diableduser)
foreach ($member in $user.MemberOf)
Write-Host “Removing” $user.SamAccountName “from” $member
Remove-ADGroupMember $member -Members $user.SamAccountName -Confirm:$false

Honorable mention for assisting on this script goes to Bjørn Wang

edit: Added script for membership removal

%d bloggers like this: