Tuesday, June 26, 2018

Saving Office 365 Login Credentials with PowerShell

I need to log into lots and lots of different Office 365 tenants all the time. Having an easier way to do this and prevent fat fingering the wrong information is a big time saver for me. This is even more the case when I use PowerShell.

I therefore decided that it would be easier to have the ability to save tenant credentials to a local file and then recall these as needed. To save the credentials to an XML file use the command:

Get-Credential | Export-CliXml -Path c:\downloads\tenant.xml

This will prompt you for a login and password as normal but then save the results into an XML in the location you specified.


If you look at the XML file created, you can see the username as expected but you’ll notice that the password has been saved securely rather than in plain text.

It is important to note here that this file now contains the access details to the tenant. You need to ensure that the file remains secure because if someone else manages to get it they maybe able to login to the tenant! Beware!

To extract the details from the file and save them into a variable you can use in PowerShell use the following command:

$credential=import-clixml -path c:\downloads\tenant.xml

now you can connect to Office 365 services as normal using:

connect-msolservice –credential $credential

and you you won’t be prompted for the login details.

Hopefully, I’ve covered all the steps in the video above, so you can see it all in action from end to end.