[英]Unable to delete files from the list using Remove-Item in PowerShell
我正在嘗試使用 Remove-Item cmdlet 從變量“$exclude_files”中刪除存儲為 InputObject 名稱的文件列表。
由於它是一個列表,我遍歷列表並獲取 InputObject 文件名。
下面是代碼:
$source_dir ="C:\Files"
#Files are in below variable $exclude_files
$exclude_files
InputObject SideIndicator
----------- -------------
Credentials.xml =>
EC2_Ubuntu.pem =>
file2.png =>
file3.txt =>
Terminals.config =>
# tried with giving path and without giving path
foreach ($i in $exclude_files){ Remove-Item -Path $source_dir $i.InputObject }
但是,我收到以下錯誤:
刪除項目:找不到路徑“C:\\Files\\file3.txt”,因為它不存在。 在 line:1 char:31 + foreach($i in $exclude_files){Remove-Item $i.InputObject} + ~~~~~~~~~~~~~~~~~~~~~~~~~ ~~ + CategoryInfo : ObjectNotFound: (C:\\Files...file3.txt:String) [Remove-Item], ItemNotFoundException +fullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand
請嘗試以下操作:
$exclude_files.InputObject | Remove-Item -Path {Join-Path $source_dir $_ }
看來, $i.InputObject
用作輸入-Filter
參數(因為這是在第一個位置參數Path
參數集),這可能不是預期的想法。
我得到了解決方案......正如你所建議的,當用 InputObject 遍歷列表時,它的工作
foreach($i in $exclude_files)
{
$i.InputObject | Remove-Item -Path {Join-Path $source_dir $_}
}
謝謝你
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.