[PS] Get-Item can't find hidden files?

Discussion in 'Scripting' started by Alex K. Angelopoulos [MVP], May 5, 2006.

  1. Is there a way to work around Get-Item not finding hidden files?

    PS> $i = get-item c:\boot.ini
    Get-Item : Could not find item C:\boot.ini.
     
    Alex K. Angelopoulos [MVP], May 5, 2006
    #1
    1. Advertisements

  2. Use the "force" :)

    [C:\temp]
    PS:8 > dir -force c:\boot.ini

    Directory: C:\

    Mode LastWriteTime Length Name
    Description
    ---- ------------- ------ ----
     
    Lee Holmes [MSFT], May 5, 2006
    #2
    1. Advertisements

  3. Ah. This really seems a bit awkward for use with Get-Item, Get-ChildItem,
    and so on.

    For example, suppose you are trying to delete a set of hidden files, but
    DON'T want to delete them if they are marked read-only. The -Force parameter
    overrides read-only as well as hidden status.
     
    Alex K. Angelopoulos [MVP], May 7, 2006
    #3
  4. Yes, and in fact that will work with hidden files. The point of this was the
    Item-ish cmdlets, though, not the content manipulation ones.
     
    Alex K. Angelopoulos [MVP], May 7, 2006
    #4
  5. Composition is your friend :)

    gci -force | remove-item

    -bruce

    --
    Bruce Payette [MSFT]
    Windows PowerShell Technical Lead
    Microsoft Corporation
    This posting is provided "AS IS" with no warranties, and confers no rights.
     
    Bruce Payette [MSFT], May 7, 2006
    #5
  6. I just _knew_ you would say that. But is this really intended? I think
    what's bothering me most is that the -Force parameter name is so generic for
    Remove-Item.


     
    Alex K. Angelopoulos [MVP], May 7, 2006
    #6
  7. In general, our approach for -Force means "do whatever you reasonably can to
    make this happen." If you keep that as the driving concept, the shell
    remains consistent. If you want to add special conditions to the operation
    (ie: not ReadOnly,) then you have to put some of the decision making powers
    in your own hand via Where-Object or something similar.

    --
    Lee Holmes [MSFT]
    Windows PowerShell Development
    Microsoft Corporation
    This posting is provided "AS IS" with no warranties, and confers no rights.


     
    Lee Holmes [MSFT], May 8, 2006
    #7
  8. I have some sympathy with Alex's concern here.

    get-childitem * -force | remove-item

    will wipe out all child items including hidden and read-only files. I
    had expected this to display a warning (even without -whatif in
    place).

    It's powerful. But it's also dangerous, in my opinion.

    Andrew Watt MVP

     
    Andrew Watt [MVP], May 11, 2006
    #8
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.