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

Dissertation

Research writing Conversely, EU Directives differ from EU Regulations in that the former are not directly applicable.Instead, they require implementation into UK law by domestic legislation (Acts of Parliament). Acts ofParliament, which have invoked the provisions of an EU Directive, will, in principle <a href="https://www.studyproessay.com/">Nursing assignment writer UK</a>  , continue to haveeffect even after formal withdrawal. However, given that there will no longer be a requirement to bringEU Directives into UK law, the UK must decide the extent to which the UK statute currently in place performing this function should be repealed, amended or simply retained.
 on 10/28/2022 3:40 AM

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

I use PowerShell frequently, and the PnP.PowerShell module has my undivided attention for the most of that time. I occasionally find that I want to add logic to my scripts based on who is running the script and who is logged in to Microsoft 365. I used this tactic for managing the tasks of https://businessbid.com/ I am so happy to have this technique.
 on 10/30/2022 7:05 AM

Nice information

I discovered a lot of useful information here. A very good post.
 on 11/8/2022 7:22 PM

Nice

I'm looking for your next post https://basketrandom.io
 on 11/8/2022 7:23 PM

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

https://gradespire.com
 Online Assignment Help and Assignment help services are the best choice for students. The students, who have a lot of assignments , essay and projects to do in their colleges will get the best assignment help services from our professional writers. They can deliver the best assignment to your email address as per your requirements.
 on 11/9/2022 12:56 PM
1 - 10Next

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