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.

tk

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

Comments

Essay help online

You have already realized that you cannot escape from essay writing assignments as it holds a significant amount of your GPA. So, contact online essay writers who can help you to top in your class?

Read More: https://myassignmenthelp.com/essay-help/essay-help-online.html
 on 1/10/2022 12:44 AM

homeworkjoy

 on 1/11/2022 12:48 AM

essay help

To pick the greatest essay help website, you need to conduct thorough research. The top essay help site may not provide the best essay aid for the work you require. To obtain your money's worth, you must read reviews, check for comments, and conduct extensive research.
https://www.edumagnate.com/essay-help.html 
 on 1/12/2022 6:03 AM

Roadrunner email

Oh Wow! Great blog I must say. The thing I like most about your blog is the catchy words that you choose. I am also here to provide information about Roadrunner email problems and settings. If you or your colleague need any help regarding rr email then you may take information from us.



http://www.email-customer-care.com/roadrunner-support

 on 1/13/2022 11:00 PM

Three Types Of Citation Styles You’ll Come Across Often

When writing an academic paper, you’ll notice many students using tools like an hurricane katrina essay often.
https://myassignmenthelp.com/us/topics/hurricane-katrina-essay.html
https://myassignmenthelp.com/us/topics/othello-iago-essay.html
 on 1/14/2022 4:06 AM

Nice post

Hi I am running an assignment company who is providing buy coursework service and proofreading service online at cheap cost for more than 10 years.
https://ivoryresearcher.com/proofreading-service
 on 1/15/2022 2:51 AM

Great

What an amazing post thanks for sharing it with us I also like writing yard because they give https://www.writingyard.com/services/capstone-project/
https://www.writingyard.com/services/btec-assignment/

 on 1/18/2022 4:33 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