PowerShell获取Windows用户列表、用户信息的方法
程序员文章站
2022-11-15 18:28:05
wmi是windows管理规范的缩写,其中包含很多系统的软硬件信息。而windows用户信息也可以通过wmi对象来获取。powershell提供了对wmi的访问功能,十分方...
wmi是windows管理规范的缩写,其中包含很多系统的软硬件信息。而windows用户信息也可以通过wmi对象来获取。powershell提供了对wmi的访问功能,十分方便且强大——这就是get-wmiobject这个cmdlet。
获取所有的windows用户列表:
get-wmiobject -class win32_useraccount 或者 get-wmiobject
复制代码 代码如下:
win32_useraccount
如果是想知道查看当前登录的用户的信息,可以用如下语句:
复制代码 代码如下:
get-wmiobject -class win32_useraccount -filter "name='$env:username' and domain='$env:userdomain'"
其中$env:username表示当前登录到系统的用户名,而$env:userdomain表示当前用户的域(或者机器名)。
上面的语句其实只显示用户的基本信息,如果想看用户更详细的信息,可以把查出来的用户当成一个对象,使用select-object方法,查看它的全部信息。
复制代码 代码如下:
get-wmiobject -class win32_useraccount -filter "name='$env:username' and domain='$env:userdomain'" | select-object *
得到的信息将像这样:
复制代码 代码如下:
status : ok
caption : hong-book\hong
passwordexpires : false
__genus : 2
__class : win32_useraccount
__superclass : win32_account
__dynasty : cim_managedsystemelement
__relpath : win32_useraccount.domain="hong-book",name="hong"
__property_count : 16
__derivation : {win32_account, cim_logicalelement, cim_managedsystemelement}
__server : hong-book
__namespace : root\cimv2
__path : \\hong-book\root\cimv2:win32_useraccount.domain="hong-book",name="hong"
accounttype : 512
description :
disabled : false
domain : hong-book
fullname :
installdate :
localaccount : true
lockout : false
name : hong
passwordchangeable : true
passwordrequired : false
sid : s-1-5-21-181061805-855091228-1216038997-1000
sidtype : 1
scope : system.management.managementscope
path : \\hong-book\root\cimv2:win32_useraccount.domain="hong-book",name="hong"
options : system.management.objectgetoptions
classpath : \\hong-book\root\cimv2:win32_useraccount
properties : {accounttype, caption, description, disabled...}
systemproperties : {__genus, __class, __superclass, __dynasty...}
qualifiers : {dynamic, locale, provider, uuid}
site :
container :
caption : hong-book\hong
passwordexpires : false
__genus : 2
__class : win32_useraccount
__superclass : win32_account
__dynasty : cim_managedsystemelement
__relpath : win32_useraccount.domain="hong-book",name="hong"
__property_count : 16
__derivation : {win32_account, cim_logicalelement, cim_managedsystemelement}
__server : hong-book
__namespace : root\cimv2
__path : \\hong-book\root\cimv2:win32_useraccount.domain="hong-book",name="hong"
accounttype : 512
description :
disabled : false
domain : hong-book
fullname :
installdate :
localaccount : true
lockout : false
name : hong
passwordchangeable : true
passwordrequired : false
sid : s-1-5-21-181061805-855091228-1216038997-1000
sidtype : 1
scope : system.management.managementscope
path : \\hong-book\root\cimv2:win32_useraccount.domain="hong-book",name="hong"
options : system.management.objectgetoptions
classpath : \\hong-book\root\cimv2:win32_useraccount
properties : {accounttype, caption, description, disabled...}
systemproperties : {__genus, __class, __superclass, __dynasty...}
qualifiers : {dynamic, locale, provider, uuid}
site :
container :
利用对象提供的这些属性信息,我们可以做很多事情,比如我们想看看当前小编登录的账号的密码是否已过期,则可以写这样一个函数:
复制代码 代码如下:
function test-userpasswordexpires
{
param(
$username = $env:username,
$domain = $env:userdomain
)
(get-wmiobject -class win32_useraccount -filter "name='$username' and domain='$domain'").passwordexpires
}
好了,关于用户信息差不多就这么多,小编祝大家使用愉快!
下一篇: [PHP]实用函数8