1

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
lit
  • 14,456
  • 10
  • 65
  • 119
  • 1
    seems like you have an encoding issue, i.e.: `–Properties` :) – Santiago Squarzon Sep 06 '22 at 00:41
  • 1
    Save ps1 file as utf8 with bom. – js2010 Sep 06 '22 at 01:10
  • To elaborate on @Santiago's and js2010's comments: in particular, the error message indicates that your BOM-less UTF-8 source-code file uses `—` (EM DASH, [`U+2014`](http://www.fileformat.info/info/unicode/char/2014)) instead of the usual ASCII-range `-` (HYPHEN-MINUS, [`U+002D`](http://www.fileformat.info/info/unicode/char/2d)). The 3-byte UTF-8 encoding of `—`, when misinterpreted by _Windows PowerShell_ as 3 individual _ANSI_ characters, yields `–`. See the linked duplicate for details. – mklement0 Sep 06 '22 at 01:56

0 Answers0