2015-11-03 150 views
4

我已經嘗試了幾種不同的方法,但它仍然不起作用。刪除所有.ica文件

remove-item .\*\appdata\local\temp\* -Include *.ica 

我希望它刪除用戶臨時文件夾中的所有.ica文件。

+0

-include和-exclude只有真正與-recurse工作。 - 過濾器應該也是如此, – Matt

回答

2

試試這個:

$path = path to the folder 
Get-ChildItem -path -Filter "*.ica" | where { ! $_.PSIsContainer } | Remove-Item -Force -Confirm:$true 
+0

文件夾的路徑是最大的問題。路徑是%temp%,但powershell似乎並不理解該路徑。 – user770022

+0

你的目標是什麼?一臺或多臺服務器?你能從廣告獲取用戶名嗎? – kekimian

2

嘗試指定根文件夾,得到匹配的文件列表,以及管道,列出來remove-item

Get-ChildItem "c:\users\$username\appdata\local\temp\*.ica" -Force -Recurse | Remove-Item -Confirm:True

- 或 -

Get-ChildItem -Path "C:\Users\$username\appdata\local\temp" -Include '.ica' -Force -Recurse | Remove-Item -Confirm:True

我想你需要-ForceGet-ChildItem,因爲那裏的一些路徑包括隱藏的文件夾。如果沒有它,它就會失效。

編輯:

foreach ($user in (Get-ChildItem -Directory C:\Users).name) { <#do stuff#> } 

此外,$ENV:Temp在PowerShell中相當於%TEMP%$ENV:中包含許多其他環境變量。

或者,如果您需要爲所有的用戶這樣做:

Get-ChildItem -Path "C:\Users\" -Include '.ica' -Force -Recurse | Remove-Item -Confirm:True

+0

兩個人都不會工作,因爲這是我將推送到企業的東西,我不知道用戶名。另外我無法使用%temp%,因爲powershell沒有看到該路徑。 – user770022

3

看起來你正在尋找用戶的臨時文件夾。如果您正在尋找,您可以使用$path = $env:temp將它用於@kekimian使用的$路徑。

成品會是這個樣子:

$path = $env:temp 
Get-ChildItem -path $path -Filter "*.ica" -Force -Recurse | Remove-Item -Force -Confirm:$False 
+0

get-ChildItem:缺少參數'Path'的參數。指定一個類型爲'System.String []'的參數並重試。 這是我得到的 – user770022

+0

對不起,請參閱我的新編輯。 – Nixphoe

+0

它的作品,但現在我得到一個彈出的確認消息。 – user770022