Getting and Setting PasswordNeverExpires Attribute for users

Filippo Iacobellis 90 Reputation points
2023-04-07T10:37:56.9666667+00:00

Hello guys, I would like to get and set the PasswordNeverExpires attribute for users. Get command I'm using right now is:
Get-MgUser -All | Format-List DisplayName, PasswordNeverExpires

Is this correct? How can I instead also set the attribute?

Microsoft Security Microsoft Graph
0 comments No comments
{count} votes

Accepted answer
  1. Gopinath Chennamadhavuni 2,446 Reputation points
    2023-04-07T15:15:07.8033333+00:00

    Hi @Filippo Iacobellis, Thanks for reaching out.

    To get list of all users and their current password expiration policy activation status, run the below command:

    Get-MgUser -All |Select-Object PasswordPolicies
    

    To Set Password Never Expire for All Users, run below code:

    Get-MgUser -All | foreach {
    $Id=$_.Id
    $DisplayName=$_.DisplayName
    Write-Progress "Set password never expires to $DisplayName"
    Update-MgUser –UserId $Id -PasswordPolicies DisablePasswordExpiration
    }
    

    To Set Password Never Expire for set of Users, run below code:

    $UserId = Import-Csv "xxx.csv" | ForEach-Object { $upn = $_."UserId" 
    Write-Progress -Activity "Setting password to never expire to -$upn"
    Update-MgUser -UserId $upn -PasswordPolicies DisablePasswordExpiration
    }
    

    Note: xxx.csv is the location of csv file contains the ID of required users.

    I have tested the commands and below the screen shot for reference.

    PScode_pS

    Hope this helps.

    If the answer is helpful, please click Accept Answer and kindly upvote. If you have any further questions about this answer, please click Comment.

    2 people found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Rohit Kumar Sinha 1,336 Reputation points
    2023-04-07T10:52:43.3733333+00:00

    HI , Please check the below url for reference of different scenarios to Get and Set password Never Expires for users https://m365scripts.com/microsoft365/set-office-365-users-password-to-never-expire-using-ms-graph-powershell/ If the above is useful please click Accept Answer.

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.