Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <#
- Recently Created Users Script
- Written by Aaron Loessberg-Zahl
- Last modified 20 November 2013
- Exports a list of recently created users and their owners.
- For comments/questions/bugs, please contact <[email protected]>
- ----------------------------------------------------------------------------
- "THE BEER-WARE LICENSE" (Revision 2659):
- <[email protected]> wrote this file. As long as you retain this
- notice, you can do whatever you want with this stuff. If we meet some day,
- and you think this stuff is worth it, you can buy me a beer in return.
- ----------------------------------------------------------------------------
- Changelog:
- v1.0 2013-11-20 amloessb Created and debugged
- #>
- <#
- .SYNOPSIS
- Exports a list of recently created users and their owners.
- .DESCRIPTION
- This script exports a list of users that were created between the specified dates, as well as their Owner according to their ACL.
- The Owner and WhenCreated properties will appear in the output by default, and additional properties may be specified in the Properties parameter.
- .PARAMETER CsvOut
- The full path of the CSV file to save the exported data to.
- .PARAMETER Start
- The earliest date to return users from.
- .PARAMETER End
- The latest date to rerurn users from.
- .PARAMETER Properties
- The list of properties to be exported.
- .PARAMETER Open
- If included, opens the exported data in Excel after it has been generated.
- .EXAMPLE
- .\Export-RecentlyCreatedUsers.ps1 -CsvOut C:\Scripts\recent.csv -Properties Name,SamAccountName,WhenCreated -Start ((Get-Date).AddDays(-5)) -End (Get-Date)
- Exports a list of users that were created in the last 5 days (120 hours).
- .EXAMPLE
- .\Export-RecentlyCreatedUsers.ps1 -CsvOut C:\Scripts\recent.csv -Properties Name,Title,Department -Start "2013/11/1" -End "2013/11/10"
- Exports a list of users that were created between midnight on 1 Nov 2013 and midnight on 10 Nov 2013.
- Note that although the property WhenCreated is not explicitly included, it will still appear in the output.
- .INPUTS
- None. You cannot pipe objects to this script.
- .OUTPUTS
- None. This script does not produce any pipeable output.
- .LINK
- None.
- #>
- Param(
- [Parameter(Mandatory = $TRUE,
- HelpMessage = "The path to save the exported CSV to.")]
- [ValidateNotNullOrEmpty()]
- [String] $CsvOut,
- [Parameter(Mandatory = $TRUE)]
- [DateTime] $Start,
- [Parameter(Mandatory = $TRUE)]
- [DateTime] $End,
- [Parameter(Mandatory = $TRUE,
- HelpMessage = "The properties to be exported.")]
- [ValidateNotNullOrEmpty()]
- [String[]] $Properties,
- [Switch] $Open
- )
- Set-StrictMode -Version 2
- $ErrorActionPreference = "Stop"
- Import-Module ActiveDirectory
- If (Test-Path (Split-Path $CsvOut -Parent)) {
- If ($Properties -notcontains "WhenCreated") { $Properties += "WhenCreated" }
- $oldDir = $PWD
- cd AD:
- Write-Progress -Activity "Export Recently Created Users" -Status "Getting accounts from AD..."
- $users = Get-ADUser -Filter * -Properties $Properties | Where {($_.WhenCreated -gt $Start) -and ($_.WhenCreated -lt $End)} |`
- Select ($Properties + @{ Name = "Owner"; Expression = { (Get-Acl $_.DistinguishedName).Owner } })
- Write-Progress -Activity "Export Recently Created Users" -Status "Saving CSV file..."
- $users | Export-Csv $CsvOut -NoTypeInformation -Force
- Write-Progress -Activity "Export Recently Created Users" -Status "Completed." -Completed
- If ($Open) {
- Start-Process excel -ArgumentList $CsvOut
- }
- cd $oldDir
- } Else {
- $Host.UI.WriteErrorLine("$(Split-Path $CsvOut -Parent) does not exist!")
- }
Advertisement
Add Comment
Please, Sign In to add comment