Difference between powershell versions

Hi
I’'ve been using this piece of code in powershell 5 without a problem

$userid = 903264
get-aduser -filter {EmployeeID -eq $userid} -properties Samaccountname

however when I use the same code in powershell 7 it throws me this error
Get-ADUser: Variable: ‘userid’ found in expression: $userid is not defined.

what do I have to change to correct this?

regards
Paul

Is this the complete code? I tried it and it works just fine for me.

And BTW: since the sAMAccountName belongs to the default subset of properties returned by Get-ADUser you don’t need to provide -Properties sAMAccountName

Hi Olaf,
this is a part of the code basically it’s a foreach where I use this piece of code in and that worked like a charm in powershell 5 however when I run it on powershell 7 it gives me this error for some reason and I can’t figure out what the reason is for this error.

this is the complete foreach

$Users = import-csv $dbworkfile # "offboarding_db.csv"
foreach($User in $Users){

    $ExpireDate = $User.Lastworkdate
    $userid = $User.Pn
    $UserTreated  = $User.treated
    
    $ADuser = Get-AdUser -filter {employeeid -eq $userid} -Properties SamAccountName|where-object {$_.Enabled -eq $TRUE} |select-object Samaccountname
    $samaccountname = $aduser.samaccountname
    $Email = $user.Email
    $SamAccount = $Email.split("@")
    $SamAccName = $Samaccount[0]
    $SamAccountName = $SamAccName
    if($aduser -and ($UserTreated -eq "No")-and ($email -ne "dummy@mydomain.com")){

        Write-log -Message "Setting account expirationdate for user $($user.name) on date $expiredate" -level info -path $logpath
        try{
          $expiredate = (get-date $expiredate).AddDays(+1)
        set-aduser -identity $samaccountname -AccountExpirationDate $expiredate
        }
        Catch{
            Write-log -Message "account expirationdate was not set for user $($user.name) invalid expirationdate $expiredate" -level warn -path $logpath 
        }
    }
}

if I use however this code

$userid = 903264
get-aduser -filter "employeeid -eq '$userid'" -properties *

then I do get the expected output

OK. So what’s the issue then? :man_shrugging:t4:

And BTW: You should NOT use "-properties * " as this puts more stress to your DC than necessary. Since you only use the sAMAccountName you can omit it. If you really need some properties not part of the default subset you should provide them explicitly.

Hi Olaf,
I know and I’m not doing that in my code this was just a test snippit.
basically the “{ }” where causing the problem here for some reason and with the double and single quotes it works which I didn’t expect hence my question here