Public/Get-AccountPWExpire.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
Function Get-AccountPWExpire
{
    <#
        .SYNOPSIS
            Lists account password expiration dates.
 
        .DESCRIPTION
            Lists account password expiration dates. This will
            list the account expiration for all users in
            an Active Directory domain.
 
        .PARAMETER Export
            If this switch is enabled, output will be directed to a CSV file.
 
        .EXAMPLE
            Get-AccountPWExpire -Export
         
        .EXAMPLE
            Get-AccountPWExpire
             
    #>


    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory=$False,Position=0,HelpMessage="If used this will export to file instead of console.")]
        [Switch]
        $Export
    )

    If($Export)
    {
        Try 
        {
            Get-ADUser -Filter {Enabled -eq $true -and PasswordNeverExpires -eq $false} â€“Properties "DisplayName", "msDS-UserPasswordExpiryTimeComputed" | Select-Object -Property "Displayname",@{Name="ExpiryDate";Expression={[datetime]::FromFileTime($_."msDS-UserPasswordExpiryTimeComputed")}} | Export-CSV AccountPWExpires.csv -NoTypeInformation
            Write-Output "AccountPWExpires.csv exported to current directory"
        }
        Catch 
        {
            Throw "Something went wrong"
        }
        
    }

    Else 
    {
        Try 
        {
            Get-ADUser -Filter {Enabled -eq $true -and PasswordNeverExpires -eq $false} â€“Properties "DisplayName", "msDS-UserPasswordExpiryTimeComputed" | Select-Object -Property "Displayname",@{Name="ExpiryDate";Expression={[datetime]::FromFileTime($_."msDS-UserPasswordExpiryTimeComputed")}} | Write-Output | Format-Table
        }
        Catch 
        {
            Throw "Something went wrong"
        }
    }

}