Windows下powershell获取指定文件夹和文件大小

将以下脚本命名为test.ps1, 并在powershell下运行.\test.ps1即可。

#list all folder and file size of specific filepath folder
function filesize ([string]$filepath)
{
    if ($filepath -eq $null)
    {
        throw "file path cannot be blank"
    }
    $_.name + "file	size(MB)" -f $l | Out-File ($filepath+"test.txt")
    dir -Path $filepath |
    ForEach-Object -Process {
        if ($_.psiscontainer -eq $true)
        {#folder size
            $length = 0
            dir -Path $_.fullname -Recurse | ForEach-Object{
                $length += $_.Length
            }
            $l = $length/1MB
            # output to the console
            $_.name + "folder size is: {0:n2} MB" -f $l
            # save to test.txt file
            $_.name + "	{0:n2}" -f $l | Out-File -Append ($filepath+"_test.txt")
        }else
        {#file size
            $length = 0
            dir -Path $_.fullname -Recurse | ForEach-Object{
                $length += $_.Length
            }
            $l = $length
            $_.name + " file size is: {0:n2} B" -f $l
            $_.name + "	{0:n2}" -f $l | Out-File -Append ($filepath+"_test.txt")
        }
    }
}
filesize -filepath "C:\Program File"

如果是第一次运行需要开启执行脚本权限。
在powershell中运行如下命令,然后 Y 确认。
开启:set-executionpolicy remotesigned
关闭:Set-ExecutionPolicy Restricted

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值