![](/img/trans.png)
[英]PowerShell Script to remove a set of file from multiple users' startup folders
[英]Powershell remove a file from multiple users' startup folders
我正在嘗試編寫腳本以從網絡上每台計算機的啟動文件夾中刪除批處理文件。 在某些計算機上,該文件位於所有用戶啟動文件夾中,因此該部分很容易。 但是,我不知道如何在每台計算機上搜索所有用戶帳戶啟動文件夾,以確保刪除文件的每個實例。 我嘗試了以下方法:
invoke-command -ComputerName $computers -ScriptBlock {remove-item -Path 'c:\Users\%username%\appdata\roaming\microsoft\windows\Start Menu\programs\startup\logon.bat' }
我以為Powershell會將%usertname%視為變量,並且只瀏覽所有用戶名,但是我錯了。 有什么辦法可以使我工作嗎?
您應該可以用*
替換%username%
,因為它接受通配符。 這將有助於檢查每個文件夾。
invoke-command -ComputerName $computers -ScriptBlock {remove-item -Path 'c:\Users\*\appdata\roaming\microsoft\windows\Start Menu\programs\startup\logon.bat' -ErrorAction SilentlyContinue }
PowerShell中的環境變量使用約定$env:variablename
。 Get-ChildItem env:
將以與“系統屬性”對話框相同的方式向您顯示所有變量。
您需要使用-ErrorAction SilentlyContinue
因為如果您沒有搜索文件夾的權限,它將失敗。 您可以使用諸如Test-Path
類的方法使它更具彈性,但這應該足夠簡單。
將-Force
和-Confirm:$false
與Remove-Item
也不會受到傷害。
怎么樣...
Foreach ($Computer in $computers){invoke-command -ComputerName $computer -ScriptBlock {
$Directories = Get-ChildItem C:\Users | ? {$_.Attributes -eq "Directory"}
ForEach($Directory in $Directories){
$File=($Directory.FullName) + "\appdata\roaming\microsoft\windows\Start Menu\programs\startup\logon.bat"
If(Test-path $File){
remove-item $File}}}}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.