Sunday 15 May 2011

Powershell Remove-Item item is Null when using get-childitem -



Powershell Remove-Item item is Null when using get-childitem -

i having problem next script, liberally makes utilize of pipelines:

$date = get-date -date "12/31/2010 00:00 am" -format mm-dd-yyyy $tdrive = "c:\users\xxxx\desktop" (get-childitem -path $tdrive -recurse -force -erroraction silentlycontinue) | ?{ $_.fullname -notmatch "\\alina_new\\?" } | ?{ $_.fullname -notmatch "\\!macros\\?" } | ?{ $_.fullname -notmatch "\\do_not_delete\\?" } | where-object {$_.lastaccesstime -lt $date} | remove-item $_

i getting next error when run this, meaning $_ null:

remove-item : cannot bind argument parameter 'path' because null. @ line:13 char:13 + remove-item $_ + ~~ + categoryinfo : invaliddata: (:) [remove-item], parameterbindingvalidationexception + fullyqualifiederrorid : parameterargumentvalidationerrornullnotallowed,microsoft.powershell.commands.r emoveitemcommand

what not understanding is, why path ever null? 1 fluid pipeline, , value of $_ should persist through entire pipeline, should not?

my thought when where-object finds file not less date, returns null through rest of pipeline. believe error happens pre-execution, however, there more fundamental problem here.

the point of script delete files not in directories listed, older 12/31/2010.

two things. first, you'll want remove-item foreach of files gci finds. second, remove-item takes path. seek this:

$date = get-date -date "12/31/2010 00:00 am" -format mm-dd-yyyy $tdrive = "c:\users\xxxx\desktop" (get-childitem -path $tdrive -recurse -force -erroraction silentlycontinue) | ?{ $_.fullname -notmatch "\\alina_new\\?" } | ?{ $_.fullname -notmatch "\\!macros\\?" } | ?{ $_.fullname -notmatch "\\do_not_delete\\?" } | where-object {$_.lastaccesstime -lt $date} | % {remove-item $_.fullname -whatif}

powershell

No comments:

Post a Comment