none
How to stop getting prompted to "Confirm" RRS feed

  • Question

  • Hello,

    I have some Windows Server 2008 systems that I'm trying to run a powershell script on to delete some temp files, but I keep getting prompted with "Confirm... Y [Yes] [A] Yes to All...... ect"  Is there a way to bypass the Confirm?

    Thanks,

    Tom


    Tom Martin Email: tmartin@caa.com
    Thursday, July 28, 2011 12:57 AM

Answers

  • Yes, if you set $confirm to false explicitly, it shouldn't prompt you to confirm.

     

    get-childitem C:\inetpub\logs\LogFiles -recurse | where-object { $_.LastAccessTime -lt (Get-Date).AddDays(-14) -and $_.LastWriteTime -lt (Get-Date).AddDays(-14) } | remove-item -ErrorAction SilentlyContinue -Confirm:$false

     

    Alternatively, you can try -force parameter as well.

    get-childitem C:\inetpub\logs\LogFiles -recurse | where-object { $_.LastAccessTime -lt (Get-Date).AddDays(-14) -and $_.LastWriteTime -lt (Get-Date).AddDays(-14) } | remove-item -ErrorAction SilentlyContinue -Force

     

    PS: Test above changes before trying out in production environment.


    Thanks,

    Sitaram Pamarthi

    Blog : http://techibee.com

    Twitter:https://twitter.com/#!/pamarths

    • Marked as answer by martit01 Monday, August 15, 2011 4:31 PM
    Thursday, July 28, 2011 4:36 AM

All replies

  • If you are using Remove-Item cmdlet use confirm switch like in below example

    Remove-Item .\v.txt -Confirm:$false


    With kind regards
    Krystian Zieja
    http://www.projectenvision.com
    Follow me on twitter
    My Blog
    Need help with your systems?
    • Proposed as answer by A.Hultgren Thursday, July 28, 2011 4:55 AM
    • Marked as answer by martit01 Thursday, July 28, 2011 4:28 PM
    • Unmarked as answer by martit01 Thursday, July 28, 2011 6:17 PM
    • Unproposed as answer by martit01 Thursday, July 28, 2011 7:03 PM
    Thursday, July 28, 2011 1:04 AM
  • Hi Krystian,

    I'm using the below command typed directly into PowerShell just for testing purposes.  So, I should just be able to add -Confirm:$false to the end and it should not prompt me to Confirm? 

    get-childitem C:\inetpub\logs\LogFiles -recurse | where-object { $_.LastAccessTime -lt (Get-Date).AddDays(-14) -and $_.LastWriteTime -lt (Get-Date).AddDays(-14) } | remove-item -ErrorAction SilentlyContinue

    Thanks,

    Tom


    Tom Martin Email: tmartin@caa.com
    Thursday, July 28, 2011 1:16 AM
  • Yes it should work by just appending -Confirm:$false at the end
    With kind regards
    Krystian Zieja
    http://www.projectenvision.com
    Follow me on twitter
    My Blog
    Need help with your systems?
    Thursday, July 28, 2011 2:24 AM
  • Yes, if you set $confirm to false explicitly, it shouldn't prompt you to confirm.

     

    get-childitem C:\inetpub\logs\LogFiles -recurse | where-object { $_.LastAccessTime -lt (Get-Date).AddDays(-14) -and $_.LastWriteTime -lt (Get-Date).AddDays(-14) } | remove-item -ErrorAction SilentlyContinue -Confirm:$false

     

    Alternatively, you can try -force parameter as well.

    get-childitem C:\inetpub\logs\LogFiles -recurse | where-object { $_.LastAccessTime -lt (Get-Date).AddDays(-14) -and $_.LastWriteTime -lt (Get-Date).AddDays(-14) } | remove-item -ErrorAction SilentlyContinue -Force

     

    PS: Test above changes before trying out in production environment.


    Thanks,

    Sitaram Pamarthi

    Blog : http://techibee.com

    Twitter:https://twitter.com/#!/pamarths

    • Marked as answer by martit01 Monday, August 15, 2011 4:31 PM
    Thursday, July 28, 2011 4:36 AM
  • I just added -Confirm:$false to the end, but when I run the command locally on the server I'm still getting the following:

    Confirm

    The item at

    Microsoft.Powershell.Core\FileSystem::C:\inetpub\logs\LogFiles\W3SVC1 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?

    [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help

    (default is “Y”):

     

    I then tried it with –Force, but I received the same Confirm prompt. 

     


    Tom Martin Email: tmartin@caa.com
    Thursday, July 28, 2011 6:26 PM
  • Looking at the command you're running.. it will remove both files and folders. If it finds an old folder, it will attempt to remove it (and its contents) regardless if the files within it has been recently accessed or modified. Is this the intent? 
    Andreas Hultgren
    MCTS, MCITP
    http://ahultgren.blogspot.com/
    Thursday, July 28, 2011 8:05 PM
  • Yes, it should remove anything older than 14 days.... files or folders / folders and files.

    Thanks,

    Tom


    Tom Martin Email: tmartin@caa.com
    Thursday, July 28, 2011 9:45 PM
  • Do you mind posting your code here for better clarity? I generally delete all temp files in c:\windows\temp with below code. I face no Confirm prompts.

    $TempFiles = Get-ChildItem c:\windows\temp -include *.tmp -recurse

    $TempFiles | foreach ($_) { remove-item $_.Fullname -force}

     


    Thanks,

    Sitaram Pamarthi

    Blog : http://techibee.com

    Follow on Twitter

    This posting is provided AS IS with no warranties or gurentees,and confers no rights

    Friday, July 29, 2011 4:38 AM
  • Yes, it should remove anything older than 14 days.... files or folders / folders and files.

    Thanks,

    Tom


    Tom Martin Email: tmartin@caa.com

    What I mean is the folders that are older than 14 days can still contain other folders and files that have been used recently. The command will try to delete that folder and its files anyway regardless of the files it contains are new or old.

     


    Andreas Hultgren
    MCTS, MCITP
    http://ahultgren.blogspot.com/
    Friday, July 29, 2011 5:17 AM
  • Here’s the code we are using to delete anything older than 14 days.  Any suggestions is much appreciated. Thanks

    get-childitem C:\inetpub\logs\LogFiles -recurse | where-object { $_.LastAccessTime -lt (Get-Date).AddDays(-14) -and $_.LastWriteTime -lt (Get-Date).AddDays(-14) } | remove-item -ErrorAction SilentlyContinue -Confirm:$false

     

     


    Tom Martin Email: tmartin@caa.com
    Friday, July 29, 2011 4:19 PM
  • This has probably already been answered but I was running into the same problem as you and couldn't find an answer.

    Maybe if I post it here I will be able to find it the next time I run into it. :-)

    I think this will work for you

    get-childitem C:\inetpub\logs\LogFiles -include *.log  -recurse | where-object { $_.LastAccessTime -lt (Get-Date).AddDays(-14) -and $_.LastWriteTime -lt (Get-Date).AddDays(-14) } | remove-item -ErrorAction SilentlyContinue -Confirm:$false

    replace *.log without ever wildcard you need.

    I was executing this

    get-childitem  -Recurse  c:\logs\ | where {$_.LastWriteTime -le $now.AddDays(-30)} | del -whatif

    and getting the same as you

    Confirm

    The item at

    Microsoft.Powershell.Core\FileSystem::C:\logs\blah 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?

    I replaced it with

    get-childitem  -Recurse  c:\logs\ -include *log*| where {$_.LastWriteTime -le $now.AddDays(-30)} | del -whatif

    and like magic no more prompts.


    Thursday, May 31, 2012 8:37 PM
  • Hello Folks,

    I'm having this issue with the "Confirm" dialog continuously popping up, no matter what combination of parameters I try.  I've been reading this blog and tried using multiple combinations, to no avail.

    Here is my command: Get-ChildItem 'c:\builds\testing' -recurse | where {$_.LastWriteTime -le (Get-Date).AddDays(0)} | del

    -ErrorAction SilentlyContinue -Confirm:$false

    If any one can help it would be greatly appreciated.

    NOTE:  I also set the $ConfirmPreference = "None"

    Thanks In Advance!


    Willie J. Pee

    Tuesday, June 19, 2012 7:05 PM
  • Hello Folks,

    I was able to solve this probably, with some internal help.

    Here is my command I used to suppress the confirm dialog that I was getting:

    Get-ChildItem c:\builds\testing -recurse | where-object {$_.LastWriteTime -le (Get-Date).AddDays(0)} | remove-item -force

    -recurse

    I hope this will be of some help to someone else that is having the same problem.

    Thanks,


    Willie J. Pee


    Tuesday, June 19, 2012 8:54 PM
  • Hello,

    Does anybody know how can I add exclusions by folder(and all its contents) to this script ?

    I have 3-4 folders and their content which I would like to keep!

    thanks!

    Sunday, June 9, 2013 3:42 PM
  • Hi,

    You can use the existing where clause to exclude the folders. If you still have questions, you're probably better off starting a new thread, as this one is old and already marked as answered.

    Sunday, June 9, 2013 8:19 PM
    Moderator
  • Thanks Willie J. Pee... this was my solution!
    Thursday, January 30, 2014 2:22 PM
  • Have been there have strugled with that ... but     -comfirm:0  has worked for me . also using  -verbose but that should be a different matter.   you could try $false= 0 , in Your site constant and variable section... 


    Wednesday, June 3, 2015 8:15 AM
  • I was having the same problem as everyone else. By removing the -confirm, and adding a -recurse after the -force; I no longer get prompted. Thanks!!!
    Friday, September 25, 2015 9:46 PM
  • Stumbled on this, and the voted answer didnt get me anywhere.  one of the below replies got me closer, but still no cookie... trying a few things based on the errors i was getting, and I stumbled onto the command to remove an item with out the prompt that i verified works in my Powershell is:

    remove-item c:\drop\ -recurse

    "c:\drop" is the folder and child objects you want removed without having to confirm.

    Wednesday, April 6, 2016 6:27 PM
  • Hi, 

    I am using in this format:

    Get-ChildItem C:\PerfLogs | where {$_.LastWriteTime -le (Get-Date).AddDays(-7)} | Remove-Item -Recurse

    You must use -Recurse .

    Tuesday, May 24, 2016 10:10 PM
  • https://technet.microsoft.com/en-us/library/ee176938.aspx

    apparently, you just need to add -recurse

    worked for me: 

    Remove-Item -path "C:\somefolder\*" -recurse


    • Edited by aedna Thursday, August 11, 2016 2:08 PM
    Thursday, August 11, 2016 2:07 PM
  • This helped my script work properly.  Without the -include I was still getting confirmation prompts.
    Wednesday, March 15, 2017 3:27 PM
  • This doesn't work
    Friday, February 2, 2018 3:28 AM
  • All of the above solutions failed for me when I was create a directory, which meant that I was prompted to OK every single directory that my script created - which was a lot. What worked for me was to apped | Out-null in order to pipe the results to Out-Null

    New-Item -ItemType Directory -Path $directory -Force:$true | Out-Null
    

    Note that $Directory is a string with the full path to the directory you want to create. Hope this saves somebody some time :P

    Tuesday, October 2, 2018 9:16 PM
  • This work perfectly. without comfirmation popup
    Friday, October 26, 2018 4:21 PM
  • This doesn't work

    Check if you have UAC turned on in control panel.  You must run the console as administrator if its at the default level.
    Monday, December 10, 2018 7:30 PM
  • This should be the real answer. It was the -Force -Recurse that fixed the issue for me.
    Wednesday, April 24, 2019 4:26 PM
  • is not working with  ErrorAction SilentlyContinue -Force  or -ErrorAction SilentlyContinue -Confirm:$false

    here is my sample code.

    $StartFolder = $args[0] #"c:\temp\deleteTest"
    $mheExlude = $args[2] #'*ExcludeFolder*'
    [decimal]$olderthen = $args[1] #'10'
    Get-ChildItem   -Path $StartFolder -Recurse | where {$_.LastWriteTime -le $(get-date).AddDays(-[decimal]-$olderthen) -and $_.FullName -notlike $mheExlude } | Remove-Item -ErrorAction SilentlyContinue -Force -WhatIf

    please advice. 

    here is how i call it.

    Powershell.exe -executionpolicy remotesigned  -File  c:\tools\PS\DeleteFilesolderthen-V.ps1 "c:\temp\deleteTest\Images" "90" "*ExcludeFolder*"


    Confirm
    The item at Microsoft.PowerShell.Core\FileSystem::c:\temp\deleteTest\Image\SubFolder 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?
    [Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"):

    is not respecting the confirmation 

    please advice.


    not for lack of tring.

    Wednesday, August 21, 2019 7:54 PM
  • is not working with  ErrorAction SilentlyContinue -Force  or -ErrorAction SilentlyContinue -Confirm:$false

    here is my sample code.

    $StartFolder = $args[0] #"c:\temp\deleteTest"
    $mheExlude = $args[2] #'*ExcludeFolder*'
    [decimal]$olderthen = $args[1] #'10'
    Get-ChildItem   -Path $StartFolder -Recurse | where {$_.LastWriteTime -le $(get-date).AddDays(-[decimal]-$olderthen) -and $_.FullName -notlike $mheExlude } | Remove-Item -ErrorAction SilentlyContinue -Force -WhatIf

    please advice. 

    here is how i call it.

    Powershell.exe -executionpolicy remotesigned  -File  c:\tools\PS\DeleteFilesolderthen-V.ps1 "c:\temp\deleteTest\Images" "90" "*ExcludeFolder*"


    Confirm
    The item at Microsoft.PowerShell.Core\FileSystem::c:\temp\deleteTest\Image\SubFolder 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?
    [Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"):

    is not respecting the confirmation 

    please advice.


    not for lack of tring.

    Hi, try removing "-WhatIf" at the end and add the -Recurse parameter to Remove-Item. 
    • Edited by A.Hultgren Wednesday, August 21, 2019 8:31 PM
    Wednesday, August 21, 2019 8:05 PM