How to remove item with ForEach-Object

I have the following code
$FilesToDelete = get-childitem "C:\Scripts\PaperOut" -recurse | where-object {$_.mode -notmatch "d"} | where-object {$_.lastwritetime -lt "2/8/2018"} | Select name

Open in new window

But when i do
$FilesToDelete | For-EachObject Remove-Item -Path C:\Scripts\PaperOut\$._Name

Open in new window

I get this error:
Remove-Item : Cannot find path 'C:\Scripts\PaperOut\@{Name=1E31D2A9BEC949379D6E99F9D2305774.XFR}.Name' because it does not exist.
At line:1 char:36
+ ... te | ForEach-Object  { Remove-Item -Path C:\Scripts\PaperOut\$_.Name}
+                            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\Scripts\Pape...05774.XFR}.Name:String) [Remove-Item], ItemNotFoundException

Open in new window

I have not been able to get just the name. Thanks for your help
namergSystems AdministratorAsked:
Who is Participating?
 
footechCommented:
However, changing your code a little.
$FilesToDelete = get-childitem "C:\Scripts\PaperOut" -recurse | where-object {$_.mode -notmatch "d"} | where-object {$_.lastwritetime -lt "2/8/2018"} | Select -expand fullname
$FilesToDelete | For-EachObject { Remove-Item -Path $_ }

Open in new window

This is better as the original won't handle files in subfolders correctly.
0
 
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
Here you are fixed.

 The issue is you wrote $._ instead of $_.

$FilesToDelete | foreach {remove-item -Path "C:\Scripts\PaperOut\$_.Name"}

Open in new window

0
 
footechCommented:
$FilesToDelete = get-childitem "C:\Scripts\PaperOut" -recurse | where-object {$_.mode -notmatch "d"} | where-object {$_.lastwritetime -lt "2/8/2018"} | Select name
$FilesToDelete | For-EachObject {Remove-Item -Path "C:\Scripts\PaperOut\$($_.Name)"}

Open in new window


You need a subexpression $() to evaluate a variable property inside a string.

Edit - sorry had some typos.
0
How do you know if your security is working?

Protecting your business doesn’t have to mean sifting through endless alerts and notifications. With WatchGuard Total Security Suite, you can feel confident that your business is secure, meaning you can get back to the things that have been sitting on your to-do list.

 
oBdACommented:
The incorrect syntax aside, you'll need a subexpression to access the property:
$FilesToDelete | For-EachObject Remove-Item -Path "C:\Scripts\PaperOut\$($_.Name)"

Open in new window

Or you use
... | Select ExpandProperty name

Open in new window

when retrieving the items.
Or, better yet, get the FullName and save yourself the trouble of repeating the path.
This will resolve the problem with files in subfolders, which you can't delete currently, even though you're using -Recurse - files in subfolders would be searched in the hartd-coded parent path.
And while you're at it, assuming PS 3.0 or later, you can use -File for Get-ChildItem, which is faster than filtering in PS.
$FilesToDelete = get-childitem "C:\Scripts\PaperOut" -recurse -File | where-object {$_.lastwritetime -lt "2/8/2018"} | Select -ExpandProperty FullName
$FilesToDelete | For-EachObject Remove-Item -Path $_.FullName"

Open in new window

0
 
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
Also, in addition, my code should have had the variable wrapped in the $(), which I meant to, and about 3 other experts mentioned above too.

  however it still holds true that, the $._Name needs to be $_.Name

$FilesToDelete | foreach {remove-item -Path "C:\Scripts\PaperOut\$($_.Name)"}

Open in new window

0
 
namergSystems AdministratorAuthor Commented:
Thank you guys.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.