none
Error in Power Shell Script, please help RRS feed

  • General discussion

  • While running this power shell i am getting the error.

    #import activedirecory module 
    Import-Module ActiveDirectory
    #import csv file 
    $csvpath = "C:\temp\expdate-list.csv"
    $users = Import-Csv -Path "$csvpath" -Encoding UTF8
    $ou = "OU=DEMO,DC=demo01,DC=it-koehler,DC=com"
    Write-Host "following users will be changed: " -ForegroundColor Gray
    foreach($user in $users )
    {
     $surname = ($user).Surname 
     $givenname = ($user).GivenName 
     $expdate = ($user).AccountExpirationDate 
     #check if date is not empty
     if($expdate -ne "$null"){
     get-aduser -Filter {(GivenName -eq $givenname) -and (Surname -eq $surname)} -SearchBase "$ou" | Set-ADUser -AccountExpirationDate $expdate
     Write-Host "expirationdate for user $givenname,$surname set to $expdate" -ForegroundColor Green
     }
     else {
     get-aduser -Filter {(GivenName -eq $givenname) -and (Surname -eq $surname)} -SearchBase "$ou" | Set-ADUser -AccountExpirationDate $null
     Write-Host "expirationdate deleted for: $givenname,$surname " -ForegroundColor Yellow
      }
     }
    #show overview 
    Write-Host " "
    Write-Host "overview:"
    Get-Aduser -Filter * -SearchBase "$ou" -Properties AccountExpirationDate | Sort-Object surname | Select-Object Surname,Givenname,AccountExpirationDate

    Error is : cannot bind parameter account expiration date cannot convert value. String was not recognized as valid date and time.

    Thursday, July 18, 2019 9:10 AM

All replies

  • Please post the complete error message. YOU have failed to post the most important parts.

    \_(ツ)_/

    Thursday, July 18, 2019 9:26 AM
  • You must not add quotes around variables.  Variables only need to be quoted wen they are merged into a string. Adding quotes breaks many things when you don't use them correctly.

    The following syntax is wrong.

    if($expdate -ne "$null"){

    This is how to do this:

    if($expdate){

    This test the variable for null.  If it is not null the result is "true".


    \_(ツ)_/

    Thursday, July 18, 2019 9:29 AM
  • You do not need to or want to do this:

    $expdate = ($user).AccountExpirationDate

    This is the correct method:

    $expdate = $user.AccountExpirationDate


    \_(ツ)_/

    Thursday, July 18, 2019 9:30 AM