How-To : Handling “tricky” file names in Powershell

This post is dedicated to handling tricky file names when trying to manipulate files, in this particular example, the name of the file cannot be changed because that in invalidate the installation - the name of the file is used as a security check to allow the installation.

If you have files with specific characters, and in this example I’m talking about the bracket character you will quickly run into problems when you try to download, copy, install or do any other file on files with this character included.

This is also just not limited to one character that is regarded as a special character.

What are the special characters do you mean?

That is indeed a very good question so let me break down all the special characters where this problem can arise:

  1. `[ ]` - Square brackets (Used for array indexing and wildcard character ranges)
  2. `{ }` - Curly braces (Used for script blocks and expression evaluation)
  3. `( )` - Parentheses (Used for grouping and order of operations)
  4. `$` - Dollar sign (Denotes variables)
  5. "` - Double quotes (String literal with variable expansion)
  6. `'` - Single quotes (String literal without variable expansion)
  7. ` ` ` - Backtick (Escape character)
  8. `#` - Hash/Pound (Comments)
  9. `|` - Pipe (Command chaining)
  10. `>` - Greater than (Redirection)
  11.  `<` - Less than (Redirection)
  12. `&` - Ampersand (Command invocation)
  13. ;` - Semicolon (Command separator)
  14. `?` - Question mark (Single character wildcard)
  15. `*` - Asterisk (Multiple character wildcard)
  16. `@` - At symbol (Array operator)
  17. `\` - Backslash (Path separator, can need escaping in certain contexts)
  18. `%` - Percent sign (Used in batch-style variable notation)
  19. `^` - Caret (Used in regular expressions)
  20. `!` - Exclamation mark (Can have special meaning in certain contexts)

What file name did I have a problem with?

I encountered this Scription caveat on the following file name, obviously, this is a product by Bomgar - I have also changed some of the numbers and letters, so it’s not the actual file name but it gives us a very good example:

bomgar-bear-w0eec30j5xf85ijeijgeyejzi1fxiw88z8zyy7hcqs0ij[8gh[8j7[8^d777c66hc90.exe

Note : If anybody is thinking, surely you can just change that to bomgar.exe and you have fixed the problem - then, no, that does not work, think of that long number of characters the security authorization code.

What is the Problem?

  1. The filename contains square brackets: `[` and `]`
  2. In PowerShell, square brackets are wildcard characters used for pattern matching
  3. When PowerShell sees these characters, it tries to interpret them as a pattern rather than literal characters
  4. This causes commands like `Copy-Item` and `Start-Process` to fail because they try to use the brackets as wildcards

Resolving the problem

If you have a file, you cannot manipulate with Powershell then that file is pretty much useless for a script and you have to manually do the job

No, not quite, there is always a solution to these problems……

For Finding/Copying the File

I have used -LiteralPath to treat all characters as literal as below:

Get-ChildItem -LiteralPath $sourcePath -Filter "bomgar-bear-w0eec30j5xf85ijeijgeyejzi1fxiw88z8zyy7hcqs0ij[8gh[8j7[8^d777c66hc90.exe"
Copy-Item -LiteralPath $sourceFile.FullName -Destination $tempPath -Force

Executing the File

Create a script block and wrap the path in quotes as below:

$scriptBlock = {
    param($path)
    Start-Process -FilePath "`"$path`"" -NoNewWindow
}
& $scriptBlock $localExePath

Why does this work?

  1. `-LiteralPath` tells PowerShell to treat all characters as literal, not special
  2. Using a script block with quoted path prevents PowerShell from interpreting the brackets
  3. The backtick (`) before quotes escapes them properly within the script block

Why have I used LiteralPath?

I have used the -LiteralPath and script block method because, if your intrested:

  1. It's more reliable across different PowerShell versions
  2. It handles all special characters, not just brackets
  3. It's clearer what the code is doing
  4. It's less prone to errors if the filename changes

Previous Post Next Post

ู†ู…ูˆุฐุฌ ุงู„ุงุชุตุงู„