Skip to content

Instantly share code, notes, and snippets.

@andrewshatnyy
Created June 25, 2012 04:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewshatnyy/2986468 to your computer and use it in GitHub Desktop.
Save andrewshatnyy/2986468 to your computer and use it in GitHub Desktop.
Used for Windows accounts clean up

Accounts-cleaner

Power Shell script that I have used for cleaning accounts mess in windows.

##Requirements

  • Exchange PowerShell
  • Outlook with your account liked to it [needed for export]

##Use it if you need

  • Export exchange account to PST file
  • Find all data folders on your network with user name [such as H:(home folders)]
  • Collect it all together on your C: drive for further archiving

###How-To: In your PowerShell window with all exchange env loaded

  • For multiple user names just pass an array of "User Names", aliases or email@domain.com separated with comma right after your alias

terminiaton.ps1 yourlogin "User Name1", uname, user@domain.com

  • For single user

terminiaton.ps1 yourlogin "User Name1"

###Don't use it if you don't like it I've been pretty busy so I couldn't write all validations for it. If you can fix some bugs please do. I won't spend any more time on that. Done with Windows, sorry :)

# Get login of an account administrator [account with mailbox]
# Assign that login to $admin variable
Param ([string]$admin, [string[]]$arrName)
# Important: Please use your login name associated wiht your outlook.
# You must have Outlook installed in order to run this script.
# If you use full name make sure you enter it with quotes.
# Example:
# teminate.ps1 ashatnyy "User1 Name"
# or
# teminate.ps1 ashatnyy "User1 Name", user2login, user3@on24.com, "User4 Name"
#
#
# Array of names to be checked and moved
# $arrName = @("Intel Events")
# Location of the archive
[string]$localPath = "C:\PSTFiles\" # <<--- replase this with your local storage of PST and file archives
# all possible paths for home data and email backup
$paths = @("\\somehost\folder\home", "\\somehost\otherfolder\data"); # <-- user's data location put as many as you want, script will itterate through
#Move folders if exists
Function MoveFolder ($userName, $remotePath){
# concat target folder name for each user
[string]$target = $localPath + $userName;
#get different names depending on what location it comes from
$remotePathArray = $remotePath.split("\");
#create folder inside local storage
if ((Test-Path $target) -and (Test-Path $remotePath)){
#Copy remote folder to renamed folder inside NAME
$localBackup = $target+"\"+$remotePathArray[-2]
write-host "Copying" $remotePath "---->>>>" $localBackup
Copy-Item -Recurse $remotePath $localBackup;
Write-Host "Removing" $remotePath;
Remove-Item -Recurse $remotePath;
}
}
Function buildPathArray($userPaths, $uName) {
# takes username and array of path and returns concatanated array
# create empty array every time function called
$pathsArray = @();
# iterrate throught the Paths
foreach($path in $userPaths){
# concatenate path with username
$userFolder = $path + $uName;
# push new element [ user path ] to an array
$pathsArray += $userFolder;
}
return $pathsArray;
}
Function CreateFolder($alias){
$target = $localPath+$alias;
if (!(Test-Path $target)){
Write-Host "Creating the folder for user data in" $target;
[IO.Directory]::CreateDirectory($target);
} else {
Write-Host "Folder" $target "in on place, no need to create!"
}
}
Function ExportMailbox($alias, $email){
# Mailbox export part
$exPath = $localPath+$alias+"\"+$alias+".pst"
Add-MailboxPermission -Identity $alias -User $admin -AccessRights FullAccess
Write-Host "Found a user "$name" with email: "$email"."
Write-Host "Will export pst to "$exPath
Export-Mailbox -Identity $alias -PSTFolderPath $exPath
}
# main body
# you can write your own validation if maibox actually exist
# I was lazy sorry
# Check if user to remove and outlok user were presented
if ($arrName -and $admin){
foreach($name in $arrName ) {
$mBx = Get-Mailbox $name | Select-Object alias, *smtp*
if(($mBx.alias) -and ($mBx.PrimarySmtpAddress)) {
$alias = $mBx.alias
$email = $mBx.PrimarySmtpAddress
# Before move Folder must exist
CreateFolder $alias;
ExportMailbox $alias $email;
#Progress bar initialize
$i = 0;
Write-Host "********************"
Write-Host "Starting with" $name
# build an array of new paths with concatenated names to it
# assigh that array to a var
$pathsArray = buildPathArray $paths $alias;
# itterate through the array of path and check folder by folder
foreach ($folder in $pathsArray){
# Progress bar
$i++;
$processing = "Processing "+ $name
Write-Progress -activity $processing -status "Forlders moved: " -PercentComplete (($i / $pathsArray.length) * 100)
#end of progress bar
# check if folder exists
if (Test-Path $folder){
# move exisiting folders to a local storage
Write-Host "User:" $name "has home folder:" $folder;
MoveFolder $alias $folder;
} else {
Write-Host "Can't find" $folder "for" $name".";
}
}
}
}
Write-Host " "
Write-Host "*************** Done ******************"
Write-Host "Please remove Exchange and AD accounts manually"
Write-Host "Make sure Voicemail and Telephone number are repurposed"
Write-Host " "
} else {
Write-Host "How-TO"
Write-Host "*******************************************************************"
Write-Host "Important: Please use your login name associated wiht your outlook."
Write-Host "You must have Outlook installed in order to run this script."
Write-Host "If you use full name make sure you enter it with quotes."
Write-Host "Example:"
Write-Host "teminate.ps1 ashatnyy "User1 Name""
Write-Host "or"
Write-Host "teminate.ps1 ashatnyy "User1 Name", user2login, user3@on24.com, "User4 Name""
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment