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
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.
Pipe the echo [y|n] to the commands in Windows PowerShell or CMD that ask “Yes/No” questions, to answer them automatically.
If the script uses Powershell cmdlets, you can specify -Confirm:$false to suppress the prompt.
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.
-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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With