PowerTip: Use the Force Switch in PowerShell


Summary: Many times Windows PowerShell cmdlets display additional information when using the Force switch.

Hey, Scripting Guy! Question How can I display hidden and system files when using Windows PowerShell?

         Hey, Scripting Guy! Answer Use the Get-Item or Get-ChildItem cmdlet and use the Force switch—this works with other cmdlets as well.

                           Get-ChildItem -force

Comments (4)

  1. Anonymous says:

    @Klaus

    I think we can use the property "Mode" to get the specifiy files or directories you want, right?

    Like hiden files

    ls -force | ?{$_.mode -match "h"}

  2. Klaus Schulte says:

    Hi Ed,

    and I still wonder why … why not add a switch -hidden ?

    Or add an "-attribute hidden" parameter ?

    There is a readonly, system, archive etc. attribute but still we have to use "-force" if we want to reveal hidden files or directories ???

    And that's … why I still wonder why … 🙂

    Klaus.

  3. Martin9700 says:

    Thought you were channeling Obi-Wan there for a moment…

  4. Ed Wilson says:

    @Klaus Schulte one reason is because -Force is a common switch that is honored by hundreds of cmdlets. To stay consistent we use standard parameters.

    @Martin9700 it is how I remember … "Use the Force ….." Then I don't forget it :-0

    @Larry Song CN good point.