Skip Ribbon Commands
Skip to main content

Quick Launch

Todd Klindt's home page > Todd Klindt's Office 365 Admin Blog > Posts > How to Get the Microsoft 365 Connected User with PnP PowerShell
October 08
How to Get the Microsoft 365 Connected User with PnP PowerShell

As I’ve alluded to before, I spend a lot of time in PowerShell, and most of it in the loving embrace of the PnP.PowerShell module. From time to time I find myself wanting to include logic in my scripts based on who the script is being run as, who connected to Microsoft 365. The majority of my connections where with the good old username and password combination. When that’s the case, I could use this to find how who I had connected as:

Connect-PnPOnline -Url https://m365x995492.sharepoint.com/
admin@M365x995492.onmicrosoft.com
((Get-PnPConnection).PSCredential).username

It looks like this:

image 

That worked great, right up to the point where I didn’t just log in with username credentials. For instance, the Sympraxis tenant requires MFA so I have to connect with the –Interactive parameter:

image 

Old Faithful let me down. Back to the drawing board. Poking around the Internet I saw some smart folks were using this method:

$ctx = Get-PnPContext
$ctx.Load($ctx.Web.CurrentUser)
$ctx.ExecuteQuery()
$ctx.Web.CurrentUser
$UPN = $ctx.Web.CurrentUser.Email

image 

I haven’t tried it yet with Certificate authentication, so I’m not sure how it reports that. The good news is that method also works with a username and password login:

image 

I’ve created a Function, Get-TKPnPCurrentUser, to make that short and easy to use. I've added it to the TKM365Commands module​ I published in GitHub.

tk

ShortURL: https://www.toddklindt.com/POSHGetCurrentPnPUser


edit 4/15/22 - Added link to GitHub

Comments

Thanks

Thank again for the wonderful work.
 on 7/12/2022 1:51 AM

The academic assistance for students

The students worldwide reach the online academic sites and virtual experts and specialists to take and avail the academic help and assistance. The platforms like https://bestassignmentwriter.co.uk/answers/care-certificate-workbook-answers/ and many other platforms are majorly known to deliver the authentic and reliable academic services.
 on 8/16/2022 3:05 AM

Comment

I've never seen so many narrative essay topics before. I may never have to waste time looking for topic ideas again. This site is worth your attention. Just click the link and find whatever topic you need.

https://proessays.net/blog/all-the-best-narrative-essay-topics-in-one-place
 on 8/22/2022 12:23 PM

T20-world-cup-2022-in-pakistan-on-daraz-app

Check Live Streaming of T20 World Cup 2022 in Pakistan on Daraz App. Daraz live is one of the T20 world cup 2022 official broadcasters.
https://t20worldcuplivescore.com/live-streaming-of-t20-world-cup-2022-in-pakistan-on-daraz-app/
 on 9/27/2022 6:15 AM

Re: How to Get the Microsoft 365 Connected User with PnP PowerShell

Amazed to read something unique kind of post. Thanks for sharing with us. I like it.
Alex,
<a href="https://www.qualityassignment.co.uk/">best assignment help</a>
 on 9/28/2022 3:29 AM

Add Comment

Items on this list require content approval. Your submission will not appear in public views until approved by someone with proper rights. More information on content approval.

Title


Body *


Today's date *

Select a date from the calendar.
Please enter today's date so I know you are a real person

Twitter


Want a message when I reply to your comment? Put your Twitter handle here.

Attachments

 

 SysKit