Powershell - display deleted objects in Active Directory

Hi EE

I was needing to find some information on deleted active directory objects and ran across this ( http://gallery.technet.microsoft.com/scriptcenter/Script-to-display-the-c995a5f6
) but I am receiving the error below .. can someone help ? or advice on what other Powershell way I can get info on deleted AD objects ?


************ Error *********
PS E:\projects\deleted> Import-Module DisplayDeletedADObjects.psm1
Import-Module : The specified module 'DisplayDeletedADObjects.psm1' was not loaded because no valid module file was found in any
module directory.
At line:1 char:1
+ Import-Module DisplayDeletedADObjects.psm1
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ResourceUnavailable: (DisplayDeletedADObjects.psm1:String) [Import-Module], FileNotFoundException
    + FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand


****** This is the Code**********
#---------------------------------------------------------------------------------
#The sample scripts are not supported under any Microsoft standard support
#program or service. The sample scripts are provided AS IS without warranty  
#of any kind. Microsoft further disclaims all implied warranties including,  
#without limitation, any implied warranties of merchantability or of fitness for
#a particular purpose. The entire risk arising out of the use or performance of  
#the sample scripts and documentation remains with you. In no event shall
#Microsoft, its authors, or anyone else involved in the creation, production, or
#delivery of the scripts be liable for any damages whatsoever (including,
#without limitation, damages for loss of business profits, business interruption,
#loss of business information, or other pecuniary loss) arising out of the use
#of or inability to use the sample scripts or documentation, even if Microsoft
#has been advised of the possibility of such damages
#---------------------------------------------------------------------------------

#requires -Version 3.0

Import-module ActiveDirectory

Function Get-OSCDeletedADObjects
{
<#
       .SYNOPSIS
        Get-OSCDeletedADObjects is an advanced function which can be used to display deleted objects in Active Directory.
    .DESCRIPTION
        Get-OSCDeletedADObjects is an advanced function which can be used to display deleted objects in Active Directory.
    .PARAMETER Name
            Specifies the name of the output object to retrieve output object.
    .PARAMETER StartTime
            Specifies the start time to retrieve output object.
    .PARAMETER EndTime
            Specifies the end time to retrieve output object.
    .PARAMETER Property
            Specifies the properties of the output object to retrieve from the server.
    .EXAMPLE
        C:\PS> Get-OSCDeletedADObjects
            
            This command shows all deleted objects in active directory.
    .EXAMPLE
          C:\PS> Get-OSCDeletedADObjects -StartTime 2/20/2013 -EndTime 2/28/2013
            
            This command shows all deleted objects in active directory from 2/20/2013 to 2/28/2013
#>
      [Cmdletbinding()]
      Param
      (
            [Parameter(Mandatory=$false,Position=0,ParameterSetName='Name')]
            [String]$Name,
            [Parameter(Mandatory,Position=1,ParameterSetName='Time')]
            [DateTime]$StartTime,
            [Parameter(Mandatory,Position=2,ParameterSetName='Time')]
            [DateTime]$EndTime,
            [Parameter(Mandatory=$false,Position=0)]
            [String[]]$Property="*"
      )
      
      $AllADObjects = Get-ADObject -Filter {(isdeleted -eq $true) -and (name -ne "Deleted Objects")} -includeDeletedObjects -property $Property
                              
      If($StartTime -and $EndTime)
      {
            $AllADObjects | Where-Object{$_.whenChanged -ge $StartTime -and $_.whenChanged -le $EndTime}      
      }
      ElseIf($Name)
      {
            $AllADObjects | Where-Object{$_."msDS-LastKnownRDN" -like $Name}
      }
      Else
      {
            $AllADObjects
      }
}
LVL 2
MilesLoganAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

footechCommented:
All the module does is add a filter for date range.  You can just run the command
Get-ADObject -Filter {(isdeleted -eq $true) -and (name -ne "Deleted Objects")} -includeDeletedObjects -property *

Open in new window

which will return all deleted objects.
1
MilesLoganAuthor Commented:
Hi Footech .. can you help me modify this so It only returns deleted obects between certain months ? ex ..deleted from Aug - Sep ?
0
footechCommented:
[datetime]$StartTime = "8/1/2013"
[datetime]$EndTime = "9/1/2013"
Get-ADObject -Filter {(isdeleted -eq $true) -and (name -ne "Deleted Objects")} -includeDeletedObjects -property whenChanged | Where-Object {$_.whenChanged -ge $StartTime -and $_.whenChanged -le $EndTime}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

SubsunCommented:
I can see the function have the parameters to add the date range..
Get-OSCDeletedADObjects -StartTime 2/20/2013 -EndTime 2/28/2013

Import-Module DisplayDeletedADObjects.psm1
Import-Module : The specified module 'DisplayDeletedADObjects.psm1' was not loaded because no valid module file was found in any module directory.
The error is because you didn't add the path of the module file in import command or you didn't copy the psm1 file to the module directory.. Try..
Import-Module C:\temp\DisplayDeletedADObjects.psm1
0
seanabCommented:
When you run the Import-Module command you need to be in the directory where the module files live or tell it where the module files live.

Also if you want to out put the results to a file you can append this to the end of the command:   > listdeletedobjects.txt
0
MilesLoganAuthor Commented:
Nice ! thanks footech
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.