get-job state blocked

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

You may need to add

-force -confirm:$false

To Remove-Item, also,

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

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

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

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

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 }