This code works well on PowerShell Core 7.2.6. Why does it fail on Windows PowerShell 5.1?
PS C:\Users\lit\bin> Get-Content -Path .\Get-ADPasswordExpirationDate.ps1
[CmdletBinding()]
Param(
[Parameter(Mandatory=$false, Position=0)]
[string[]]$UserNames = @($Env:USERNAME)
)
Write-Verbose "Getting password expiration date for user $UserNames"
ForEach ($UserName in $UserNames) {
Get-ADUser -identity $UserName –Properties "DisplayName", "msDS-UserPasswordExpiryTimeComputed" |
Select-Object -Property `
@{Name='UserName';Expression={$_.SamAccountName}},
"Displayname",
@{Name='Email';Expression={$_.UserPrincipalName}},
@{Name="ExpiryDate";Expression={[datetime]::FromFileTime($_."msDS-UserPasswordExpiryTimeComputed")}}
}
PS C:\Users\lit\bin> .\Get-ADPasswordExpirationDate.ps1
At C:\Users\lit\bin\Get-ADPasswordExpirationDate.ps1:15 char:112
+ ... {[datetime]::FromFileTime($_."msDS-UserPasswordExpiryTimeComputed")}}
+ ~~~~
The string is missing the terminator: ".
At C:\Users\lit\bin\Get-ADPasswordExpirationDate.ps1:9 char:35
+ ForEach ($UserName in $UserNames) {
+ ~
Missing closing '}' in statement block or type definition.
+ CategoryInfo : ParserError: (:) [], ParseException
+ FullyQualifiedErrorId : TerminatorExpectedAtEndOfString
PS C:\Users\lit\bin> $PSVersionTable.PSVersion.ToString()
5.1.19041.1682