Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Script to delete users for AD and Exchange

$
0
0

I have this powershell script that is not running and I can not figure out why.  Can someone please help.

 cls
$ExpiredUsers=Get-ADUser -Filter * -SearchBase "OU=OverExpired,OU=Disabled,OU=Guests,DC=College,DC=EDU"
ForEach($ExpiredUser in $ExpiredUsers){
 $UserToDelete=Get-ADUser $ExpiredUser -Properties HomeDirectory|Select *
 If($UserToDelete.HomeDirectory -ne $null){
  # Write-Host "The home folder is defined."
  $HomeFolder=$UserToDelete.HomeDirectory
  $HomeFolder = $HomeFolder.Replace("server1","server2")
  If(Test-Path $HomeFolder){
   # Write-Host "We have a home folder that exists."
   $HomeRootLength=$HomeFolder.Length - $UserToDelete.SamAccountName.Length
   $HomeRoot=$HomeFolder.Substring(0,$HomeRootLength)
   $ArchiveFolder=$HomeRoot + "Archive"

   Move-Item $HomeFolder $ArchiveFolder
  }
 }
 $MBXtoDelete=Get-Mailbox -Identity $UserToDelete.DistinguishedName
 If($MBXtoDelete -eq $null){
  # Write-Host "No Exchange Mailbox; deleting AD account."
  Remove-ADUser -Identity $UserToDelete.DistinguishedName -Confirm:$false
 }
 Else{
  # Write-Host "Deleting Exchange Mailbox."
  Remove-Mailbox -Identity $UserToDelete.DistinguishedName -Confirm:$false
 }
}

 

These are the errors I'm recieving when I try and run the script in PowerGUI script Editor.

 

The term 'Get-ADUser' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verif
y that the path is correct and try again.
At C:\Accounts\Scriptorium\DeleteExpiredADAccounts.ps1:16 char:25
+ $ExpiredUsers=Get-ADUser <<<<  -Filter * -SearchBase "OU=OverExpired,OU=Disabled,OU=Guests,DC=College,DC=EDU"
    + CategoryInfo          : ObjectNotFound: (Get-ADUser:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
 
The term 'Get-ADUser' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verif
y that the path is correct and try again.
At C:\Accounts\Scriptorium\DeleteExpiredADAccounts.ps1:18 char:26
+     $UserToDelete=Get-ADUser <<<<  $ExpiredUser -Properties HomeDirectory|Select *
    + CategoryInfo          : ObjectNotFound: (Get-ADUser:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
 
The term 'Get-Mailbox' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, veri
fy that the path is correct and try again.
At C:\Accounts\Scriptorium\DeleteExpiredADAccounts.ps1:32 char:26
+     $MBXtoDelete=Get-Mailbox <<<<  -Identity $UserToDelete.DistinguishedName
    + CategoryInfo          : ObjectNotFound: (Get-Mailbox:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
 
The term 'Remove-ADUser' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, ve
rify that the path is correct and try again.
At C:\Accounts\Scriptorium\DeleteExpiredADAccounts.ps1:35 char:16
+         Remove-ADUser <<<<  -Identity $UserToDelete.DistinguishedName -Confirm:$false
    + CategoryInfo          : ObjectNotFound: (Remove-ADUser:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException


Viewing all articles
Browse latest Browse all 6937

Trending Articles