Code to login and logout a user from Office 365

%3CLINGO-SUB%20id%3D%22lingo-sub-225573%22%20slang%3D%22en-US%22%3ECode%20to%20login%20and%20logout%20a%20user%20from%20Office%20365%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-225573%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20all%2C%3C%2FP%3E%3CP%3EFirst%20time%20here...%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20am%20trying%20to%20write%20a%20code%20that%20will%20automatically%26nbsp%3Blogout%20the%20current%20Office%20365%20user%20from%20the%20account%20on%20the%26nbsp%3Bpc%2C%20and%20another%20option%20to%20login%20a%20certain%20user%20to%20Office%20365.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI'm%20trying%20to%20do%20it%20in%20powershell%2C%20in%20which%20I%20do%20not%20have%20much%20experience%2C%20but%20I%20cannot%20find%20some%20matching%20code%20examples%20to%20start%20with.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHas%20anyone%20has%20such%20code%20to%20help%20me%20get%20started%2C%20please%3F%3C%2FP%3E%3CP%3EOther%20related%20ideas%20are%20mostly%20welcome%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-225573%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EOffice%20365%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ESharePoint%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-225672%22%20slang%3D%22en-US%22%3ERe%3A%20Code%20to%20login%20and%20logout%20a%20user%20from%20Office%20365%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-225672%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20Hanouch%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20would%20like%20to%20start%20answering%20your%20question%20with%20a%20question.%20%3A)%3C%2Fimg%3E%26nbsp%3B%20Why%20do%20you%20want%2F%20this%20need%20this%3F%20Could%20you%20give%20some%20more%20context%20as%20of%20why%20you%20are%20creating%20a%20custom%20login%2F%20logout%20procedure%3F%20Perhaps%20an%20alternate%20solution%20could%20help%20you%20better.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIf%20a%20(your)%20workspace%20environment%20is%20setup%20correctly%20(using%20for%20example%20SSO)%20users%20are%20logged%20in%20automatically%20into%20Office%20365%2C%20no%20code%20is%20necessary.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBut%20if%20you%20want%20to%20FORCE%20logout%20a%20certain%20user(s)%2C%20the%20following%20might%20help%20you%20on%20your%20way%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CUL%3E%3CLI%3E%3CSTRONG%3EBrowser%2F%20UI%3C%2FSTRONG%3E%3CUL%3E%3CLI%3EIn%20Office%20365%20Admin%20center%2C%20use%20the%20following%20the%20following%20steps.%3CBR%20%2F%3EOffice%20365%20Admin%20Center%3CBR%20%2F%3EUsers%20-%26gt%3B%20Active%20Users%20-%26gt%3BSelect%20the%20User%20and%20in%20the%20OneDrive%20settings%2C%20click%20Initiate%20sign%20out%3C%2FLI%3E%3C%2FUL%3E%3C%2FLI%3E%3CLI%3E%3CSTRONG%3EPowershell%3C%2FSTRONG%3E%3CUL%3E%3CLI%3E%3CDIV%3EImport-Module%20MSOnline%3C%2FDIV%3E%3CDIV%3E%24cred%20%3D%20Get-Credential%3CBR%20%2F%3EConnect-MsolService%20-Credential%20%24cred%3CBR%20%2F%3EConnect-SPOService%20-Url%20%3CADD%20you%3D%22%22%20office365%3D%22%22%20spo%3D%22%22%20admin%3D%22%22%20url%3D%22%22%20here%3D%22%22%3E%20-Credential%20%24cred%3C%2FADD%3E%3C%2FDIV%3E%3CDIV%3E%23%20To%20get%20all%20enabled%20users%3CBR%20%2F%3E%24users%20%3D%20Get-MsolUser%20-EnabledFilter%20EnabledOnly%20-All%3C%2FDIV%3E%3CDIV%3E%23%20To%20get%20all%20the%20users%3CBR%20%2F%3E%23%24users%20%3D%20Get-MsolUser%3B%3CBR%20%2F%3Eforeach%20(%20%24user%20in%20%24users)%7B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20Revoke-SPOUserSession%20-user%20%24user.UserPrincipalName%20-Confirm%3A%24false%3CBR%20%2F%3E%7D%3C%2FDIV%3E%3C%2FLI%3E%3C%2FUL%3E%3C%2FLI%3E%3C%2FUL%3E%3CP%3E%3CEM%3ENote%3A%3C%2FEM%3E%3C%2FP%3E%3CP%3EThe%20user(s)%20that%20you%20FORCE%20logout%20can%20immediately%20log%20back%20in%20unless%20you%20have%20blocked%20their%20account.%20Also%20when%20using%20the%20Powershell%20option%20you%20need%20to%20be%20Global%20admin.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHope%20this%20helps%20and%20I'm%20looking%20forward%20in%20reading%20some%20more%20context%20as%20of%20why%20you%20would%20want%20to%20write%20such%20a%20script.%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1159015%22%20slang%3D%22en-US%22%3ERe%3A%20Code%20to%20login%20and%20logout%20a%20user%20from%20Office%20365%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1159015%22%20slang%3D%22en-US%22%3E%3CP%3EThe%20PS%20commands%20you%20gave%20above%20are%20for%20an%20admin%20to%20force%20logout%20of%20multiple%20people.%20Can%20you%20furnish%20commands%20that%20would%20allow%20me%20to%20log%20myself%20out%20only%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20use%20case%20is%20this%3A%20if%20I%20log%20in%20at%209am%20on%20Monday%2C%20it%20logs%20me%20out%20automatically%20one%20day%20later.%20If%20I'm%20in%20a%20Teams%20meeting%20at%20that%20time%2C%20it%20forces%20me%20to%20exit%20the%20meeting%2C%20which%20can%20be%20very%20disruptive.%20To%20avoid%20that%20disruption%20I%20need%20to%20remember%20to%20log%20out%20and%20re-log%20in%20before%20the%20meeting.%20I%20would%20far%20prefer%20to%20be%20logged%20out%20automatically%20at%20a%20fixed%20time%20every%20day%2C%20say%205am%2C%20so%20when%20I%20start%20work%20I%20am%20forced%20to%20log%20in%20immediately%20and%20then%20don't%20have%20to%20worry%20about%20it%20for%20the%20remainder%20of%20the%20day.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20know%20that%20my%20IT%20department%20could%20do%20this%2C%20but%20they've%20got%20bigger%20fish%20to%20fry%20at%20the%20moment%2C%20so%20I%20need%20this%20script.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
Occasional Visitor

Hi all,

First time here...

 

I am trying to write a code that will automatically logout the current Office 365 user from the account on the pc, and another option to login a certain user to Office 365.

 

I'm trying to do it in powershell, in which I do not have much experience, but I cannot find some matching code examples to start with.

 

Has anyone has such code to help me get started, please?

Other related ideas are mostly welcome

 

2 Replies
Highlighted

Hi Hanouch,

 

I would like to start answering your question with a question. :)  Why do you want/ this need this? Could you give some more context as of why you are creating a custom login/ logout procedure? Perhaps an alternate solution could help you better.

 

If a (your) workspace environment is setup correctly (using for example SSO) users are logged in automatically into Office 365, no code is necessary. 

 

But if you want to FORCE logout a certain user(s), the following might help you on your way:

 

  • Browser/ UI
    • In Office 365 Admin center, use the following the following steps.
      Office 365 Admin Center
      Users -> Active Users ->Select the User and in the OneDrive settings, click Initiate sign out
  • Powershell
    • Import-Module MSOnline
      $cred = Get-Credential
      Connect-MsolService -Credential $cred
      Connect-SPOService -Url <add you office365 SPO admin url here> -Credential $cred
      # To get all enabled users
      $users = Get-MsolUser -EnabledFilter EnabledOnly -All
      # To get all the users
      #$users = Get-MsolUser;
      foreach ( $user in $users){
          Revoke-SPOUserSession -user $user.UserPrincipalName -Confirm:$false
      }

Note:

The user(s) that you FORCE logout can immediately log back in unless you have blocked their account. Also when using the Powershell option you need to be Global admin.

 

Hope this helps and I'm looking forward in reading some more context as of why you would want to write such a script. 

The PS commands you gave above are for an admin to force logout of multiple people. Can you furnish commands that would allow me to log myself out only?

 

The use case is this: if I log in at 9am on Monday, it logs me out automatically one day later. If I'm in a Teams meeting at that time, it forces me to exit the meeting, which can be very disruptive. To avoid that disruption I need to remember to log out and re-log in before the meeting. I would far prefer to be logged out automatically at a fixed time every day, say 5am, so when I start work I am forced to log in immediately and then don't have to worry about it for the remainder of the day.

 

I know that my IT department could do this, but they've got bigger fish to fry at the moment, so I need this script.