Windows Vista Forums

How to supress confirmation when using Remove-Item
  1. #1


    Flea# Guest

    How to supress confirmation when using Remove-Item

    Hello,

    I am wanting to remove some files from a directory based on some criteria
    and the script works, however, I don't want the confirmation to prompt, I
    just want the files to delete, however, when I include the -confirmation
    parameter, PowerShell throws an error saying it can't match it. Here is what
    I currently have that is not working:

    Remove-Item -path .\. -confirm A | Get-ChildItem | Where { $_.Length -Le
    1000000 }

    "A" is suppose to be the "Yes to All" and that is what I want to pass. What
    is the correct syntax for this?



    Thanks,
    Flea#
    --
    http://fleasharp.blogspot.com/

      My System SpecsSystem Spec

  2. #2


    Brandon Shell [MVP] Guest

    Re: How to supress confirmation when using Remove-Item

    The default behavior is to NOT prompt.

    Remove the -Confirm

    And your logic looks backwards

    Get-ChildItem | Where { $_.Length -Le 1000000} | remove-item -force -whatif


    REMOVE the whatif if this does what you expect.

    Brandon Shell
    ---------------
    Blog: http://www.bsonposh.com/
    PSH Scripts Project: www.codeplex.com/psobject

    F> Hello,
    F>
    F> I am wanting to remove some files from a directory based on some
    F> criteria and the script works, however, I don't want the confirmation
    F> to prompt, I just want the files to delete, however, when I include
    F> the -confirmation parameter, PowerShell throws an error saying it
    F> can't match it. Here is what I currently have that is not working:
    F>
    F> Remove-Item -path .\. -confirm A | Get-ChildItem | Where { $_.Length
    F> -Le 1000000 }
    F>
    F> "A" is suppose to be the "Yes to All" and that is what I want to
    F> pass. What is the correct syntax for this?
    F>
    F> Thanks,
    F> Flea#



      My System SpecsSystem Spec

  3. #3


    Flea# Guest

    Re: How to supress confirmation when using Remove-Item

    Thanks Brandon, I switched it around as you said and that seemed to fix the
    issue. I did it this way and it worked as you said.

    Get-ChildItem -Filter *.log | Where { $_.Length -Le 1000000 } | Remove-Item

    Thanks,
    Flea#
    --
    http://fleasharp.blogspot.com/


    "Brandon Shell [MVP]" wrote:

    > The default behavior is to NOT prompt.
    >
    > Remove the -Confirm
    >
    > And your logic looks backwards
    >
    > Get-ChildItem | Where { $_.Length -Le 1000000} | remove-item -force -whatif
    >
    >
    > REMOVE the whatif if this does what you expect.
    >
    > Brandon Shell
    > ---------------
    > Blog: http://www.bsonposh.com/
    > PSH Scripts Project: www.codeplex.com/psobject
    >
    > F> Hello,
    > F>
    > F> I am wanting to remove some files from a directory based on some
    > F> criteria and the script works, however, I don't want the confirmation
    > F> to prompt, I just want the files to delete, however, when I include
    > F> the -confirmation parameter, PowerShell throws an error saying it
    > F> can't match it. Here is what I currently have that is not working:
    > F>
    > F> Remove-Item -path .\. -confirm A | Get-ChildItem | Where { $_.Length
    > F> -Le 1000000 }
    > F>
    > F> "A" is suppose to be the "Yes to All" and that is what I want to
    > F> pass. What is the correct syntax for this?
    > F>
    > F> Thanks,
    > F> Flea#
    >
    >
    >

      My System SpecsSystem Spec

  4. #4


    Kiron Guest

    Re: How to supress confirmation when using Remove-Item

    To complement Brandon's answer:

    -Confirm is a [Switch] parameter. These parameters can either be present ($true) or not ($false), i.e., no argument can be pass to them. You can set it to either state like this:

    $confirm = $true
    Get-ChildItem | Where {$_.Length -Le 1000000} | Remove-Item -confirm:$confirm
    # is equivalent to:
    Get-ChildItem | Where {$_.Length -Le 1000000} | Remove-Item -confirm

    $confirm = $false
    Get-ChildItem | Where {$_.Length -Le 1000000} | Remove-Item -confirm:$confirm
    # is equivalent to:
    Get-ChildItem | Where {$_.Length -Le 1000000} | Remove-Item

    --
    Kiron

      My System SpecsSystem Spec

  5. #5


    Hal Rottenberg Guest

    Re: How to supress confirmation when using Remove-Item

    Kiron wrote:

    > To complement Brandon's answer:
    To complement Kiron's

    > -Confirm is a [Switch] parameter. These parameters can either be present
    > ($true) or not ($false), i.e., no argument can be pass to them. You can
    > set it to either state like this:

    > $confirm = $false
    > Get-ChildItem | Where {$_.Length -Le 1000000} | Remove-Item
    > -confirm:$confirm
    In the case above, you can pass $false as a parameter in cases where you
    want to be positive you won't be prompted, for example in a production
    script. Another way to do the same thing is this:

    Remove-Item -Confirm:$false



    --
    Hal Rottenberg <hal@xxxxxx>
    Author, TechProsaic (http://halr9000.com)
    Webmaster, Psi (http://psi-im.org)
    Co-host, PowerScripting Podcast (http://powerscripting.net)

      My System SpecsSystem Spec

How to supress confirmation when using Remove-Item problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
Remove confirmation notice on emptying folders Barry Vista mail 4 05 Mar 2008
Remove-Item issues.. JMinahan PowerShell 4 16 Mar 2007
Remove-Item bug? Brandon Shell PowerShell 1 26 Feb 2007
Remove-item on a variable name using a regex Marco Shaw PowerShell 2 07 Dec 2006
remove-item and ~ bug? Andrew Watt [MVP] PowerShell 2 21 Jul 2006