Get-Item $Path where $Path contains underscore

Dick Watson 81 Reputation points
2021-09-18T17:10:21.77+00:00

Files exists in a folder. Get-Item [folder]* returns them. They include files with underscores embedded in their names. E.g.:

PS C:\Users\myuser> get-item "C:\Users\myuser\dest\CloudBkup\source\homes\me\archive\2021\zzzz\pre-closing disclosure\*"


    Directory: C:\Users\myuser\dest\CloudBkup\source\homes\me\archive\2021\zzzz\pre-closing disclosure


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---l         8/27/2021   3:22 PM       (192226) RE_ [SECURE] Re_ xxxx_ yyyy_ Closing Disclosure
                                                  2.pdf.AES.7z
-a---l         9/13/2021  11:29 AM       (197474) Re_ [SECURE] Re_ xxxx_ yyyy_ Closing Disclosure
                                                  20200830.pdf.AES.7z
-a---l         8/26/2021  11:00 AM       (193746) RE_ [SECURE] Re_ xxxx_ yyyy_ Closing
                                                  Disclosure.pdf.AES.7z

Problem is that when I try to do a Get-Item (or Remove-Item) on these, individually, by name, nothing happens.

PS C:\Users\myuser> get-item "C:\Users\myuser\dest\CloudBkup\source\homes\me\archive\2021\zzzz\pre-closing disclosure\RE_ [SECURE] Re_ xxxx_ yyyy_ Closing Disclosure.pdf.AES.7z"
PS C:\Users\myuser>

The underscore is the problem, somehow, but what is the fix? This is occurring in a script where the .FullName of the item is being invoked, e.g.,

Remove-Item $vRmFile.FullName

So, that's the point at which I need to somehow make the underscores PowerShell safe. I've Googled this but can't seem to find any answer that reflects this general case; many are about how to replace the underscores with something else.

Thanks in advance for any help!

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,435 questions
{count} votes

Accepted answer
  1. Andreas Baumgarten 100.8K Reputation points MVP
    2021-09-18T18:08:14.517+00:00

    Hi @Dick Watson ,

    the underscores aren't the issue. The square brackets [ and ] in the file name causing the trouble with Get-Item.

    You can try this:

    $file = 'C:\Junk\RE_ [SECURE] Re_ xxxx_ yyyy_ Closing Disclosure.pdf.AES.7z'  
    Get-Item -Literalpath $file  
      
    $path  = "C:\Junk"  
    Get-ChildItem $path -File | ForEach-Object {Get-Item -Literalpath $path\$_}  
      
    # Output Get-Item  
      
    Mode                 LastWriteTime         Length Name  
    ----                 -------------         ------ ----  
    -a----        18.09.2021     19:55             11 RE_ [SECURE] Re_ xxxx_ yyyy_ Closing Disclosure.pdf.AES.7z  
      
    # Output Get-ChildItem  
      
    Mode                 LastWriteTime         Length Name  
    ----                 -------------         ------ ----  
    -a----        27.03.2021     20:07          33592 ResultServices.txt  
    -a----        18.09.2021     19:55             11 RE_ [SECURE] Re_ xxxx_ yyyy_ Closing Disclosure.pdf.AES.7z  
    

    And this will delete the file without any problems:

    $file = 'C:\Junk\RE_ [SECURE] Re_ xxxx_ yyyy_ Closing Disclosure.pdf.AES.7z'  
    Get-Item -Literalpath $file | Remove-Item  
    

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten


3 additional answers

Sort by: Most helpful
  1. Andreas Baumgarten 100.8K Reputation points MVP
    2021-09-18T18:50:59.103+00:00

    Hi @Dick Watson ,

    Please try these 3 options (all are working here):

    $file = 'C:\Junk\1\RE_ [SECURE] Re_ xxxx_ yyyy_ Closing Disclosure.pdf.AES.7z'  
    # Option1  
    Get-Item -Literalpath $file | Remove-Item  
    # Option 2  
    $a = Get-Item -Literalpath $file  
    $a | Remove-Item  
    # Option 3  
    $a = Get-Item -Literalpath $file  
    Remove-Item -LiteralPath $a  
    

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten


  2. Rich Matheisen 45,431 Reputation points
    2021-09-18T18:57:51.847+00:00

    As Andreas pointed out, the "[" and "]" characters are the reason for this happening. More specifically, most people assume that a "wildcard" character is only an asterisk. That assumption leads the unwary into trouble! PowerShell "wildcard" characters are "*" (asterisk), "?" (question mark), and the "[]" pair (range).

    You can, as Andreas pointed out, use "-LiteralPath" instead of "-Path", or you can surround your path string with single-quotes instead of double-quotes (which disables wildcards and variable interpolation (i.e. embedded strings that begin with "$" which may be interpreted as the name of a variable).

    However, the problem isn't obvious when you're using the full name of the file that's found in the property of another object. For example, the results of Get-ChildItem. In your case (where you've implicitly used the -Path parameter), you should be using the -LiteralPath parameter and not the -Path parameter (which is the default) in the Remove-Item.

    For more detail, have a look at this: about_wildcards


  3. Dick Watson 81 Reputation points
    2021-09-18T19:07:33.417+00:00

    (I'm at a bit of a loss whether this "Q&A" UI thinks we should reply, not answer, with Comment or "Write an Answer". Why Comment even exists is a bit of a mystery to me. It seems like Microsoft reinvented a wheel here with "Q&A" without looking at any other wheels to see how other people do them.)