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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.