Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell New-Item: How to Accept Confirmation Automatically

I'm trying to create a new directory on a network drive using a powershell script but it keeps prompting me

"Confirm: Are you sure you want to perform this action ...."

Is there a way to override this so it doesn't ask me since I'm running this script from a web interface.

Here is my call:

New-Item $rollbackDirectory -type Directory -Force

It does the same thing, with or without the -Force parameter

I've also tried this format with no luck

New-Item -name $rollbackName -itemtype directory -path $rollbackdrive -Debug -Force
like image 240
Evan Layman Avatar asked Aug 04 '11 23:08

Evan Layman


People also ask

How do I Auto confirm in PowerShell?

You have to use the -Confirm switch to the command to prompt the user for confirmation. It forces the command to display the confirmation prompt in PowerShell. A simpler way to use the -Confirm switch would be by using it in the Remove-Item cmdlet, which can produce a confirmation action in simpler command.

How do I automatically answer yes to a prompt in PowerShell?

Pipe the echo [y|n] to the commands in Windows PowerShell or CMD that ask “Yes/No” questions, to answer them automatically.

How do I bypass a PowerShell script confirmation?

If the script uses Powershell cmdlets, you can specify -Confirm:$false to suppress the prompt.

What does the Confirm parameter do in PowerShell?

The Confirm switch instructs the command to which it is applied to stop processing before any changes are made. The command then prompts you to acknowledge each action before it continues.


2 Answers

-confirm need only be specified when you want the cmdlet to prompt you for confirmation. Whether the cmdlet by itself would prompt for confirmation or not depends on the developer of the cmdlet who can set a high, medium, low for the cmdlet based on its effect. Based on the value of $ConfirmPreference you will get the confirmation automatically for a cmdlet. The default value for $ConfirmPreference is high and the level set for New-Item is medium. So if the New-Item is prompting for confirmation, the $ConfirmPreference value must have been changed to medium or low.

Change it using $ConfirmPreference="high" or even $ConfirmPreference="none" to make New-Item not prompt, or your solution of -confirm:$false works as well by overriding the $ConfirmPreference.

Explained perfectly here: http://blogs.msdn.com/b/powershell/archive/2006/12/15/confirmpreference.aspx

Hope this clears it up.

like image 99
manojlds Avatar answered Sep 21 '22 10:09

manojlds


I ended up trying this (even though I read on another SO post that it is not correct):

New-Item $rollbackDirectory -type Directory -Force -Confirm:$false

And it worked! Hope this helps others with the same issue

like image 32
Evan Layman Avatar answered Sep 21 '22 10:09

Evan Layman