Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

When writing PowerShell scripts, I noticed when certain cmdlets encounter problems they bring up an interactive prompt, Remove-Item on a non-empty directory being an example. This is deadly when attempting to automate tasks, I'd much rather the action just fail and either throw an exception or return a bad return code so that the entire script isn't locked up waiting for a response.

Is there any way to force PowerShell to automatically fail as opposed to seeking user input on actions?

share|improve this question
    
I think you should rename your question "How can I make Remove-Item fail fast?" –  Jay Bazuzi Mar 27 '13 at 20:38
    
This isn't specifically about Remove-Item, it's just an illustrative example. There are lots of other cmdlets that will block waiting for user input under the right conditions. –  Chuu Mar 27 '13 at 21:17

3 Answers 3

See Get-Help about_Preference_Variables:

$ConfirmPreference
------------------
    Determines whether Windows PowerShell automatically prompts you for
    confirmation before running a cmdlet or function.

...

None:    Windows PowerShell does not prompt automatically.
                         To request confirmation of a particular command, use
                         the Confirm parameter of the cmdlet or function.

So:

> $ConfirmPreference = 'None'
share|improve this answer
    
I found that to see the problem I had to set. $ConfirmPreference = 'Low' –  Guy Thomas Mar 26 '13 at 11:00
    
This does not appear to work. Remove-Item on a directory still gives you a prompt after setting $ConfirmPreference to 'None' or 'Low' –  Chuu Mar 27 '13 at 19:07

Ok, This is really ugly, but holy mustard stains it "works".

Issues:

  • I have not figured out how to continue after the first error
  • In this case, it removes plain files, then stops at the first folder
  • I have not figured out how to make this cmdlet work if I add the UseTransaction parameter
  • This will only work for the Simple case (commands that don't do a lot of stuff with the current environment). I have not tested anything complex

    $MyPS = [Powershell]::Create()
    $MyPS.Commands.AddCommand("Remove-Item")
    $MyPS.Commands.AddParameter("Path", "D:\Temp\t")
    $MyPS.Invoke()

Output:

Commands                                                                                                                 
--------                                                                                                                 
{Remove-Item}                                                                                                            
{Remove-Item}                                                                                                            
Exception calling "Invoke" with "0" argument(s): "A command that prompts the user failed because the host program or the 
command type does not support user interaction. The host was attempting to request confirmation with the following 
message: The item at D:\Temp\t has children and the Recurse parameter was not specified. If you continue, all children 
will be removed with the item. Are you sure you want to continue?"
At line:19 char:1
+ $MyPS.Invoke()
+ ~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : CmdletInvocationException
share|improve this answer
    
The reason I haven't marked this as a solution is I haven't found time to test it. Has anyone deployed or tested this code besides the author? –  Chuu Oct 30 '13 at 15:36

I suggest two techniques

a) Append -force

b) Append -errorAction silently continue

This is how I research which cmdlets support a particular parameter

Get-Command | where { $_.parameters.keys -contains "erroraction"}
share|improve this answer
    
One of the reasons I started down this path is that I usually want to do the opposite of -force, i.e. something like -alwaysbackoff. I've found that most commands that support -force don't seem to have any option to back off though. –  Chuu Mar 24 '13 at 8:39
    
As an aside, a convenient abbreviation for -errorAction SilentlyContinue is -ea 0. –  Jay Bazuzi Mar 25 '13 at 3:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.