PowerShell snips


An assortment of PowerShell scripts I have stumbled upon, modified where needed. Use at your own risk blah blah. :)
Sorry if I don’t give credit, but I simply can’t remember where I got them all from!
Many of them require Quest ActiveRoles Management Shell, an extension for Windows PowerShell (freeware).

Generic PowerShell

Read .CSV file and do something for each object:
ForEach ($usr in Get-Content “C:\temp\Users.csv”) { Get-Mailboxstatisti
cs($usr)}

Groups/AD users

Copy Group Members from Group A to B:
Get-QADGroupMember GroupA | % { Add-QADGroupMember GroupB -Member $_ }

Copy Group Membership information from User A to B:
(Get-QADUser userA).MemberOf | Add-QADGroupMember -Member domain\userB

“Bulk” change attributes on AD users:
Get-QADuser -sizelimit 0 | where-object { $_.Department -eq “Sales” } | foreach-object {Set-QADuser $_ -Department “Commercial”}
(Searches all users (sizelimit 0) where Department = “Sales” and for each object sets Department = “Commercial”)

Find “indirect” (nested) AD group memberships for a specific user:
Get-QADMemberOf “John Doe” -indirect | fl name,description >> C:\JDoe.txt

Find members of AD group, export to CSV:
get-qadgroupmember “group name” | select-object logonname, firstname, lastname | export-csv c:\filename.csv

Export selected ExtensionAttribute (or other) to CSV file

Get-QADUser -IncludedProperties extensionAttribute1 -sizeLimit 0 | Select-Object Name, samaccountname, extensionAttribute1 | Out-File C:\WhoIsWho.csv -width 400

Blank out ExtensionAttribute1 for all users that have it
Get-QADuser -sizelimit 0 | Set-QADUser -ObjectAttribute @{extensionAttribute1=$null}

Select users by country CAUTION, do not edit in this way!
Get-QADuser -sizelimit 0 -IncludedProperties c | Where-Object {$_.C -eq “GB” }

 

Exchange 2007/2010

Set Exchange mailbox quota:
set-mailbox “John Doe” -UseDatabaseQuotaDefaults $false -IssueWarningQuota 4GB -ProhibitSendQuota 5GB -ProhibitSendReceiveQuota 6GB
(Warning at 4GB, Block Send at 5GB and block receive at 6GB, remember to NOT use QuotaDefaults)

Export mailbox statistics to CSV file
C:\>Get-MailboxStatistics | ft Displayname,totalitemsize,databasename | Out-File C:\Mailboxstatistics.csv -width 200

Export mailboxes to .PST file
Add-mailboxpermission -identity “John Doe” -accessrights fullaccess -user “AdminUser01″
Export-Mailbox -Identity “John Doe” -PSTFolderpath X:\folder
(AdminUser01 has to be the one who’s running the Powershell at the moment of Exporting)

Removing disconnected mailboxes in Exchange Server 2007/2010
Listing all disconnected mailboxes:
Get-MailboxStatistics | where-object { $_.DisconnectDate -ne $null } | Select DisplayName,MailboxGuid
Removing a single entry:
Remove-Mailbox -Database <Database-Name> -StoreMailboxIdentity <MailboxGuid> -confirm:$false
Removing all users at the same time:
$users = Get-MailboxStatistics | where-object { $_.DisconnectDate -ne $null } | Select DisplayName,MailboxGuid
Now that we have all disconnected mailboxes in a var, we can run the following cmdlet to remove all of them:
$users | ForEach { Remove-Mailbox -Database “Mailbox Database” -StoreMailboxIdentity $_.MailboxGuid -confirm:$true }

 

, ,

  1. No comments yet.
(will not be published)
*


Follow

Get every new post on this blog delivered to your Inbox.

Join other followers: