简体   繁体   中英

How to output specific information from a powershell command?

So I've been playing around with Powershell recently, trying some things with a basic command net user $username . As an example net user administrator produces an output that you can see at the bottom of this page.

My question is: How do I output specific elements of this?

I'm aware of pipes and have been trying to use them but I think I'm missing something as it never comes out right. Lets say, for example, I just want user name , full name , password expires and last logon to be shown as an output. What command do I use after the pipe to get this?

Many thanks!

net.exe is not a PowerShell cmdlet. Therefore getting information out it is processing the output of the executable as a string.

For example, retrieving the user name:

$netoutput = net user administrator
#User name is on the first line, separated by spaces, the actual username is last word
$netoutput[1].Split(" ")[-1]

If you are using Win10 1607 or newer, you could retrieve this information with the Get-LocalUser cmdlet

Get-LocalUser administrator | Select-Object Name,FullName,PasswordExpires,LastLogon

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM