我有以下代码来获取与microsoft帐户相关联的全名。从技术上讲,它是有效的,但应该有一种方法可以简洁地做到这一点,而无需重新定义变量。
$fullName = Net User $Env:username | Select-String -Pattern "Full Name";$fullName = ("$fullName").TrimStart("Full Name")
前修剪:
net user $env:username | findstr 'Full Name'
Full Name The Admin
使用带有参数的方法运行的foreach-object或%的版本。TrimStart()区分大小写
net user $env:username | findstr 'Full Name' | % trimstart Full` Name
The Admin
Select-object将有Line属性中的字符串。
net user $env:username | select-string 'Full Name' | % { $_.line.
trimstart('Full Name') }
The Admin
或者取select-string在字符串上下文中的输出:
net user $env:username | select-string 'Full Name' |
% { "$_".trimstart('Full Name') }
The Admin
或者只是get-localuser和fullname属性:
localuser $env:username | % fullname
The Admin
trimstart如何工作的另一个演示;每个字母都是独立的,顺序无关紧要:
'FFNNuuee hi there' | % trimstart 'Full Name'
hi there