找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 6506|回复: 3

[问题求助] 求助PowerShell显示文件夹的大小

[复制链接]
发表于 2025-5-21 15:08:06 | 显示全部楼层 |阅读模式
  1. Get-ChildItem -Path "D:\hyper-v-bak" -Directory | ForEach-Object {
  2.     $size = (Get-ChildItem -Path $_.FullName -Recurse -Force | Measure-Object -Property Length -Sum).Sum
  3.     [PSCustomObject]@{
  4.         Directory = $_.FullName
  5.         Size = "{0:N2}" -f ($size / 1GB) + "GB" # 以GB为单位显示大小
  6.     }
  7. }
复制代码
直接复制到powershell运行没有问题,但是保存到ps1,运行之后好像闪退一样。想达到的效果就是,点击运行ps1文件显示出这个文件夹下面文件夹的大小,请大佬看看到底是哪里出了问题,感谢。
或者有成熟的bat代码分享以下。
发表于 2025-5-21 16:30:10 | 显示全部楼层
本帖最后由 aloha20200628 于 2025-5-21 18:21 编辑

回复 1# wqm491201

以下代码是 bat+powershell 混编版本,存为 test.bat 运行...

  1. <# ::
  2. @echo off &powershell -c "iex(${%~f0}|out-string)" &pause&exit/b
  3. #>
  4. $fso=New-Object -com Scripting.FileSystemObject
  5. dir 'D:\hyper-v-bak' -dir -r | % {
  6.    $z=$fso.GetFolder($_.FullName).size
  7.    ('{0:N2}'-f($z/1GB))+"GB`t"+$_.fullname
  8. }
  9. exit
复制代码
以上代码第4-9行存为 test.ps1 运行(最后一行 exit 改为 timeout /t -1)亦可...
发表于 2025-5-22 01:18:55 | 显示全部楼层
本帖最后由 1139054012 于 2025-5-22 12:22 编辑

&#9989; 1.手动指定跨路径版
  1. <# ::
  2. @echo off
  3. SETLOCAL EnableDelayedExpansion

  4. :: 获取当前脚本路径
  5. set "BAT_SCRIPT_PATH=%~dp0"
  6. set "BAT_SCRIPT_FILE=%~f0"

  7. :: 创建安全的临时文件名(避免特殊字符问题)
  8. for /f "tokens=2 delims==" %%I in ('wmic os get localdatetime /value') do set "DATETIME=%%I"
  9. set "TEMP_PS_FILE=%TEMP%\ps_script_%DATETIME:~0,14%.ps1"

  10. :: 更可靠地提取PowerShell代码(跳过所有bat行)
  11. findstr /v /r "^@.* ^:.*" "%BAT_SCRIPT_FILE%" > "%TEMP_PS_FILE%"

  12. :: 执行PowerShell脚本并正确处理错误级别
  13. powershell.exe -NoProfile -ExecutionPolicy Bypass -File "%TEMP_PS_FILE%" %*
  14. set "EXIT_CODE=%ERRORLEVEL%"

  15. :: 清理临时文件
  16. del /f /q "%TEMP_PS_FILE%" >nul 2>&1

  17. :: 使用正确的退出代码退出
  18. pause>nul /b %EXIT_CODE%

  19. #>
  20. # PowerShell脚本从这里开始
  21. Get-ChildItem -Path "D:\【test测试特殊路径】\C盘" -Directory | ForEach-Object {
  22.     $size = (Get-ChildItem -Path $_.FullName -Recurse -Force -ErrorAction SilentlyContinue |
  23.             Measure-Object -Property Length -Sum -ErrorAction SilentlyContinue).Sum
  24.    
  25.     [PSCustomObject]@{
  26.         Directory = $_.FullName
  27.         Size = if ($size) { "{0:N2}GB" -f ($size / 1GB) } else { "0GB" }
  28.     }
  29. } | Format-Table -AutoSize
复制代码
&#9989; 2.自动获取bat下文件夹版
  1. <# ::
  2. @echo off
  3. SETLOCAL EnableDelayedExpansion

  4. :: 获取当前脚本路径
  5. set "BAT_SCRIPT_PATH=%~dp0"
  6. set "BAT_SCRIPT_FILE=%~f0"

  7. :: 创建安全的临时文件名(避免特殊字符问题)
  8. for /f "tokens=2 delims==" %%I in ('wmic os get localdatetime /value') do set "DATETIME=%%I"
  9. set "TEMP_PS_FILE=%TEMP%\ps_script_%DATETIME:~0,14%.ps1"

  10. :: 更可靠地提取PowerShell代码(跳过所有bat行)
  11. findstr /v /r "^@.* ^:.*" "%BAT_SCRIPT_FILE%" > "%TEMP_PS_FILE%"

  12. :: 执行PowerShell脚本并正确处理错误级别
  13. powershell.exe -NoProfile -ExecutionPolicy Bypass -File "%TEMP_PS_FILE%" %*
  14. set "EXIT_CODE=%ERRORLEVEL%"

  15. :: 清理临时文件
  16. del /f /q "%TEMP_PS_FILE%" >nul 2>&1

  17. :: 使用正确的退出代码退出
  18. pause
  19. #>

  20. # PowerShell脚本从这里开始
  21. # 自动获取当前目录
  22. $currentPath = if ($args[0]) { $args[0] } else { $PWD.Path }

  23. # 获取当前目录下的所有文件夹并计算大小
  24. Get-ChildItem -Path $currentPath -Directory | ForEach-Object {
  25.     $folder = $_
  26.     try {
  27.         $size = (Get-ChildItem -Path $folder.FullName -Recurse -Force -ErrorAction Stop |
  28.                 Measure-Object -Property Length -Sum -ErrorAction Stop).Sum
  29.     } catch {
  30.         $size = 0
  31.     }
  32.    
  33.     [PSCustomObject]@{
  34.         文件夹名称 = $folder.Name
  35.         大小_GB = if ($size) { "{0:N2}" -f ($size / 1GB) } else { "0" }
  36.         文件数量 = (Get-ChildItem -Path $folder.FullName -Recurse -File -Force -ErrorAction SilentlyContinue).Count
  37.         修改时间 = $folder.LastWriteTime.ToString("yyyy-MM-dd HH:mm")
  38.         完整路径 = $folder.FullName
  39.     }
  40. } | Sort-Object 大小_GB -Descending | Format-Table -AutoSize
复制代码
&#9989; 3. 手动输入路径版
  1. <# ::
  2. @echo off
  3. SETLOCAL EnableDelayedExpansion

  4. :: 获取当前脚本路径
  5. set "BAT_SCRIPT_PATH=%~dp0"
  6. set "BAT_SCRIPT_FILE=%~f0"

  7. :: 创建安全的临时文件名(避免特殊字符问题)
  8. for /f "tokens=2 delims==" %%I in ('wmic os get localdatetime /value') do set "DATETIME=%%I"
  9. set "TEMP_PS_FILE=%TEMP%\ps_script_%DATETIME:~0,14%.ps1"

  10. :: 更可靠地提取PowerShell代码(跳过所有bat行)
  11. findstr /v /r "^@.* ^:.*" "%BAT_SCRIPT_FILE%" > "%TEMP_PS_FILE%"

  12. :: 提示用户输入路径
  13. set /p "inputPath=请输入要分析的文件夹路径(直接回车使用当前目录): "

  14. :: 执行PowerShell脚本并正确处理错误级别
  15. powershell.exe -NoProfile -ExecutionPolicy Bypass -File "%TEMP_PS_FILE%" "%inputPath%"
  16. set "EXIT_CODE=%ERRORLEVEL%"

  17. :: 清理临时文件
  18. del /f /q "%TEMP_PS_FILE%" >nul 2>&1

  19. :: 使用正确的退出代码退出
  20. pause
  21. exit /b %EXIT_CODE%
  22. #>

  23. # PowerShell脚本从这里开始
  24. param(
  25.     [string]$inputPath
  26. )

  27. # 如果用户没有输入路径,则使用当前目录
  28. if ([string]::IsNullOrEmpty($inputPath)) {
  29.     $currentPath = $PWD.Path
  30. } else {
  31.     $currentPath = $inputPath
  32. }

  33. # 检查路径是否存在
  34. if (-not (Test-Path -Path $currentPath -PathType Container)) {
  35.     Write-Host "错误: 路径 '$currentPath' 不存在或不是一个文件夹"
  36.     exit 1
  37. }

  38. # 获取当前目录下的所有文件夹并计算大小
  39. Get-ChildItem -Path $currentPath -Directory | ForEach-Object {
  40.     $folder = $_
  41.     try {
  42.         $size = (Get-ChildItem -Path $folder.FullName -Recurse -Force -ErrorAction Stop |
  43.                 Measure-Object -Property Length -Sum -ErrorAction Stop).Sum
  44.     } catch {
  45.         $size = 0
  46.     }
  47.    
  48.     [PSCustomObject]@{
  49.         文件夹名称 = $folder.Name
  50.         大小_GB = if ($size) { "{0:N2}" -f ($size / 1GB) } else { "0" }
  51.         文件数量 = (Get-ChildItem -Path $folder.FullName -Recurse -File -Force -ErrorAction SilentlyContinue).Count
  52.         修改时间 = $folder.LastWriteTime.ToString("yyyy-MM-dd HH:mm")
  53.         完整路径 = $folder.FullName
  54.     }
  55. } | Sort-Object 大小_GB -Descending | Format-Table -AutoSize
复制代码
发表于 2025-5-26 20:44:39 | 显示全部楼层
回复 1# wqm491201

补遗》
一楼代码存为 *.ps1 文件被双击一闪而过的解法,可先尝试在一楼代码末尾加一行 timeout /t -1 暂停即可;若此举无效,则很可能是系统设置没有就绪(默认设置是不允许双击运行 *.ps1 文件),须在 powershell 管理员窗口运行如下命令,以便获权允许双击运行 *.ps1 文件 ...

  1. set-executionpolicy remotesigned -force
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-17 06:23 , Processed in 0.018480 second(s), 8 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表