gpt4 book ai didi

powershell - 使用 powershell 检索用户配置文件的大小

转载 作者:行者123 更新时间:2023-12-02 23:52:56 24 4
gpt4 key购买 nike

我正在尝试检索 Windows 机器中配置文件的确切大小。

下面是我的代码和O/P

$profiles = Get-ChildItem C:\Users | ?{Test-path C:\Users\$_\NTUSER.DAT} | Select -ExpandProperty Name
foreach($profile in $profiles)
{
$largeprofile = Get-ChildItem C:\Users\$profile -recurse | Measure-Object -Sum length | Select -ExpandProperty Sum
$largeprofile = [math]::Round(($largeprofile/1MB),2) + "MB"
if($largeprofile -lt 20){Continue}
$object = New-Object -TypeName PSObject
$object | Add-Member -MemberType NoteProperty -Name Name -Value $profile
$object | Add-Member -MemberType NoteProperty -Name "Size(MB)" -Value $largeprofile
($object | fl | Out-String).Trim();Write-Output "`n"
}

O/P

Name : admin

Size(MB) : 34.62

但是文件夹的确切大小为 181MB,powershell 无法读取父文件夹内的所有文件夹和文件,我如何才能获得文件夹属性中显示的确切大小。

注意:对于配置文件文件夹以外的文件夹,o/p 是正确的。

最佳答案

递归目录时,您必须将参数-Force 添加到Get-ChildItem。来自文档 Get-ChildItem -Force 参数:

Allows the cmdlet to get items that cannot otherwise not be accessed by the user, such as hidden or system files.

此外,您需要添加 -ErrorAction SilentlyContinue,这样您就不会被 Access Denied 错误淹没。这些更改使您的代码看起来像这样:

$profiles = Get-ChildItem C:\Users | ?{Test-path C:\Users\$_\NTUSER.DAT} | Select -ExpandProperty Name
foreach($profile in $profiles)
{
$largeprofile = Get-ChildItem C:\Users\$profile -Recurse -Force -ErrorAction SilentlyContinue | Measure-Object -Sum length | Select -ExpandProperty Sum
$largeprofile = [math]::Round(($largeprofile/1MB),2) + "MB"
if($largeprofile -lt 20){Continue}
$object = New-Object -TypeName PSObject
$object | Add-Member -MemberType NoteProperty -Name Name -Value $profile
$object | Add-Member -MemberType NoteProperty -Name "Size(MB)" -Value $largeprofile
($object | fl | Out-String).Trim();Write-Output "`n"
}

关于powershell - 使用 powershell 检索用户配置文件的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54539189/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com