amloessb

Export-RecentlyCreatedUsers.ps1

Nov 20th, 2013
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2. Recently Created Users Script
  3. Written by Aaron Loessberg-Zahl
  4. Last modified 20 November 2013
  5.  
  6. Exports a list of recently created users and their owners.
  7.  
  8. For comments/questions/bugs, please contact <[email protected]>
  9.  
  10. ----------------------------------------------------------------------------
  11. "THE BEER-WARE LICENSE" (Revision 2659):
  12. <[email protected]> wrote this file. As long as you retain this
  13. notice, you can do whatever you want with this stuff. If we meet some day,
  14. and you think this stuff is worth it, you can buy me a beer in return.
  15. ----------------------------------------------------------------------------
  16.  
  17. Changelog:
  18. v1.0    2013-11-20      amloessb        Created and debugged
  19. #>
  20.  
  21. <#
  22. .SYNOPSIS
  23. Exports a list of recently created users and their owners.
  24.  
  25. .DESCRIPTION
  26. This script exports a list of users that were created between the specified dates, as well as their Owner according to their ACL.
  27.  
  28. The Owner and WhenCreated properties will appear in the output by default, and additional properties may be specified in the Properties parameter.
  29.  
  30. .PARAMETER CsvOut
  31. The full path of the CSV file to save the exported data to.
  32.  
  33. .PARAMETER Start
  34. The earliest date to return users from.
  35.  
  36. .PARAMETER End
  37. The latest date to rerurn users from.
  38.  
  39. .PARAMETER Properties
  40. The list of properties to be exported.
  41.  
  42. .PARAMETER Open
  43. If included, opens the exported data in Excel after it has been generated.
  44.  
  45. .EXAMPLE
  46. .\Export-RecentlyCreatedUsers.ps1 -CsvOut C:\Scripts\recent.csv -Properties Name,SamAccountName,WhenCreated -Start ((Get-Date).AddDays(-5)) -End (Get-Date)
  47.  
  48. Exports a list of users that were created in the last 5 days (120 hours).
  49.  
  50. .EXAMPLE
  51. .\Export-RecentlyCreatedUsers.ps1 -CsvOut C:\Scripts\recent.csv -Properties Name,Title,Department -Start "2013/11/1" -End "2013/11/10"
  52.  
  53. Exports a list of users that were created between midnight on 1 Nov 2013 and midnight on 10 Nov 2013.
  54. Note that although the property WhenCreated is not explicitly included, it will still appear in the output.
  55.  
  56. .INPUTS
  57. None. You cannot pipe objects to this script.
  58.  
  59. .OUTPUTS
  60. None. This script does not produce any pipeable output.
  61.  
  62. .LINK
  63. None.
  64. #>
  65.  
  66. Param(
  67.     [Parameter(Mandatory = $TRUE,
  68.                HelpMessage = "The path to save the exported CSV to.")]
  69.     [ValidateNotNullOrEmpty()]
  70.     [String] $CsvOut,
  71.     [Parameter(Mandatory = $TRUE)]
  72.     [DateTime] $Start,
  73.     [Parameter(Mandatory = $TRUE)]
  74.     [DateTime] $End,
  75.     [Parameter(Mandatory = $TRUE,
  76.                HelpMessage = "The properties to be exported.")]
  77.     [ValidateNotNullOrEmpty()]
  78.     [String[]] $Properties,
  79.     [Switch] $Open
  80. )
  81.  
  82. Set-StrictMode -Version 2
  83. $ErrorActionPreference = "Stop"
  84.  
  85. Import-Module ActiveDirectory
  86.  
  87. If (Test-Path (Split-Path $CsvOut -Parent)) {
  88.     If ($Properties -notcontains "WhenCreated") { $Properties += "WhenCreated" }
  89.    
  90.     $oldDir = $PWD
  91.    
  92.     cd AD:
  93.    
  94.     Write-Progress -Activity "Export Recently Created Users" -Status "Getting accounts from AD..."
  95.     $users = Get-ADUser -Filter * -Properties $Properties | Where {($_.WhenCreated -gt $Start) -and ($_.WhenCreated -lt $End)} |`
  96.              Select ($Properties + @{ Name = "Owner"; Expression = { (Get-Acl $_.DistinguishedName).Owner } })
  97.    
  98.     Write-Progress -Activity "Export Recently Created Users" -Status "Saving CSV file..."
  99.     $users | Export-Csv $CsvOut -NoTypeInformation -Force
  100.    
  101.     Write-Progress -Activity "Export Recently Created Users" -Status "Completed." -Completed
  102.    
  103.     If ($Open) {
  104.         Start-Process excel -ArgumentList $CsvOut
  105.     }
  106.    
  107.     cd $oldDir
  108. } Else {
  109.     $Host.UI.WriteErrorLine("$(Split-Path $CsvOut -Parent) does not exist!")
  110. }
Advertisement
Add Comment
Please, Sign In to add comment