Windows/PowerShell

Windows/PowerShell

Basics

输入 EOF : (等效于 Linux 下 Ctrl + D)

Ctrl + Z, enter

允许未签名 PS 代码执行

设置里将 PowerShell 相关的未签名代码执行设为允许。

然后开始菜单里搜索 PowerShell, 右键 run as administrator 打开 PS然后运行

Set-ExecutionPolicy Bypass

(即使已经彻底禁用了UAC也必须用 run as administrator 方式打开 PS,否则无权限,傻逼微软!)

常用脚本

Remove all empty folders recursively

https://stackoverflow.com/questions/28631419/how-to-recursively-remove-all-empty-folders-in-powershell

# A script block (anonymous function) that will remove empty folders
# under a root folder, using tail-recursion to ensure that it only
# walks the folder tree once. -Force is used to be able to process
# hidden files/folders as well.
$tailRecursion = {
    param(
        $Path
    )
    foreach ($childDirectory in Get-ChildItem -Force -LiteralPath $Path -Directory) {
        & $tailRecursion -Path $childDirectory.FullName
    }
    $currentChildren = Get-ChildItem -Force -LiteralPath $Path
    $isEmpty = $currentChildren -eq $null
    if ($isEmpty) {
        Write-Verbose "Removing empty folder at path '${Path}'." -Verbose
        Remove-Item -Force -LiteralPath $Path
    }
}

使用:

& $tailRecursion -Path 'C:\a'

Last update: 2023-05-15 02:56:17 UTC