如何使用 power shell 脚本从目录中排除文件夹
How to exclude folders from a directory using power shell script
我有一个包含几个文件夹的目录,我想排除包含字符串的文件夹名称。我尝试使用以下命令使用通配符选项(在排除变量中)在排除部分不起作用。
$dirName = "C:\Users\xyz\Desktop\powershellScripts\destmonitors"
$excludes = "PortMonitor","ProcessMonitor","FileWatcher","UrlMonitor","*SQLMonitor*","LogMonitor"
Get-ChildItem "C:\Users\xyz\Desktop\powershellScripts\monitors\*" -Directory | Where-Object{$_.Name -notin $excludes} | Copy-Item -Destination $dirName -Recurse -Force
示例:- 假设我在 SQLMonitor、CSTK123_SQLMonitor、TEST_SQLMonitor、SQLMonitor_abc 等监视器目录中有文件夹......现在我想使用通配符排除这些文件夹
如何在 PowerShell 中实现这一点。
$dirName = "C:\Users\sj01856\Desktop\powershellScripts\destmonitors"
$excludes = "PortMonitor","ProcessMonitor","FileWatcher","UrlMonitor","*SQLMonitor*","LogMonitor"
Get-ChildItem "C:\Users\sj01856\Desktop\powershellScripts\monitors\*" -Directory | Where-Object{$_.Name -notmatch ($excludes -join "|")} | Copy-Item -Destination $dirName -Recurse -Force
您需要根据数组进行匹配,而不是按字面意思使用星号。
您应该能够在 Get-ChildItem
调用中完成所有这些操作,使用 -Excludes
参数,该参数需要一组要排除的模式:
$dirName = "C:\Users\xyz\Desktop\powershellScripts\destmonitors"
$excludes = "PortMonitor","ProcessMonitor","FileWatcher","UrlMonitor","*SQLMonitor*","LogMonitor"
Get-ChildItem "C:\Users\xyz\Desktop\powershellScripts\monitors" -Directory -Exclude $excludes |
Copy-Item -Destination $dirName -Recurse -Force
我有一个包含几个文件夹的目录,我想排除包含字符串的文件夹名称。我尝试使用以下命令使用通配符选项(在排除变量中)在排除部分不起作用。
$dirName = "C:\Users\xyz\Desktop\powershellScripts\destmonitors"
$excludes = "PortMonitor","ProcessMonitor","FileWatcher","UrlMonitor","*SQLMonitor*","LogMonitor"
Get-ChildItem "C:\Users\xyz\Desktop\powershellScripts\monitors\*" -Directory | Where-Object{$_.Name -notin $excludes} | Copy-Item -Destination $dirName -Recurse -Force
示例:- 假设我在 SQLMonitor、CSTK123_SQLMonitor、TEST_SQLMonitor、SQLMonitor_abc 等监视器目录中有文件夹......现在我想使用通配符排除这些文件夹
如何在 PowerShell 中实现这一点。
$dirName = "C:\Users\sj01856\Desktop\powershellScripts\destmonitors"
$excludes = "PortMonitor","ProcessMonitor","FileWatcher","UrlMonitor","*SQLMonitor*","LogMonitor"
Get-ChildItem "C:\Users\sj01856\Desktop\powershellScripts\monitors\*" -Directory | Where-Object{$_.Name -notmatch ($excludes -join "|")} | Copy-Item -Destination $dirName -Recurse -Force
您需要根据数组进行匹配,而不是按字面意思使用星号。
您应该能够在 Get-ChildItem
调用中完成所有这些操作,使用 -Excludes
参数,该参数需要一组要排除的模式:
$dirName = "C:\Users\xyz\Desktop\powershellScripts\destmonitors"
$excludes = "PortMonitor","ProcessMonitor","FileWatcher","UrlMonitor","*SQLMonitor*","LogMonitor"
Get-ChildItem "C:\Users\xyz\Desktop\powershellScripts\monitors" -Directory -Exclude $excludes |
Copy-Item -Destination $dirName -Recurse -Force