Author Posts

May 22, 2015 at 7:19 am

Hello everyone I'm trying to build a script in part of the script it will search for a specific filename in drive and will delete it if found I created a job
start-job -scriptblock { Get-ChildItem d:\ -Include $filename -Recurse -Force | Remove-Item }
any idea what could be missing I checked technet they only mentioned that blocked means it's waiting user intervention any help please

Best

May 22, 2015 at 7:23 am

Best guess, you've got a directory that's matching your $filename as well, the directory contains subfolders or files, and you haven't used the -Recurse parameter, which results in a prompt for the user.

If you only want this to target files rather than directories, you can do this one of two ways:

# On Powershell 3.0 or later, just use the -File switch to GetChildItem:
Get-ChildItem d:\ -Include $filename -Recurse -Force -File | Remove-Item 

# In PowerShell 2.0, filter with Where-Object
Get-ChildItem d:\ -Include $filename -Recurse -Force | Where-Object { -not $_.PSIsContainer } | Remove-Item 

May 22, 2015 at 7:27 am

You may need to add

-force -confirm:$false

To Remove-Item, also,

May 22, 2015 at 7:58 am

start-job -scriptblock { Get-ChildItem -path e:\ -Include example.txt -Recurse -Force -confirm:$false | Remove-Item -force -confirm:$false }
the state now failed I created a small virtual drive e:\ and copied many folders in it and in each folder contains example.txt just for texting purpose but nothing happend and state is failed

May 22, 2015 at 7:58 am

Try running Receive-Job, to see what the errors were.

May 22, 2015 at 8:07 am

PS E:\> get-job -name job36 | receive-job -keep
A parameter cannot be found that matches parameter name 'confirm'.
+ CategoryInfo : InvalidArgument: (:) [Get-ChildItem], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
+ PSComputerName : localhost

May 22, 2015 at 8:09 am

Ah, that's only on Remove-Item, not Get-ChildItem.

May 22, 2015 at 8:10 am

Remove-Item may need the switch, not Get-ChildItem:

start-job -scriptblock { Get-ChildItem -path e:\ -Include example.txt -Recurse  | Remove-Item -force -confirm:$false }