locked
Powershell script with New-Item -Value and square brackets doesn't work RRS feed

  • Question

  • Hello,

    i tried to make a simple script which gets all files in folders and creates a symlink for all of them:

    $files = (Get-ChildItem -LiteralPath "$FilePath" -Recurse -File).Name
    
    Foreach ($file in $files) {
    
    New-Item -ItemType SymbolicLink -Path "$LinkPath\$file" -Value "$FilePath\$file"
    
    }

    Unfortunately the actual files have square brackets in the name ('[' , ']') and there is always an error that the file could not be found:

    New-Item : Der Pfad "<Path>\test[0].txt" kann nicht gefunden werden, da er nicht vorhanden ist.
    In <path>\Create_Symbolic_Links.ps1:70 Zeichen:5
    +     New-Item -ItemType SymbolicLink -Path ($LinkPath + '\' + $nsp.Rep ...
    +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [New-Item], PSInvalidOperationException
        + FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.NewItemCommand

    I already searched for many hints and tried replacing the brackets with backslash and one or multiple ticks (`) but nothing lead to a solution.
    Also there is no -LiteralPath in the New-Item for the "-Value" option (-Path option seems to work fine).

    I have PS-Version 5.1.18362.628

    Any suggestions?

    Regards

    Friday, April 3, 2020 9:52 PM

Answers

  • Thank you @BOfH-666 That helped understand everything better.

    Still, the issue was a lot simpler. 

    Because of the -Recurse and the files sitting in subfolders of the $FilePath, the Path in the -Value parameter wasn't correct (missing the subfolder).
    I still had to replace the square brackets with two ticks and then it worked.

    $files = Get-ChildItem -Path "$FilePath" -Recurse -File -Exclude *.log,*.ps1 
    
    Foreach ($file in $files) {
        
        $name = $file.name
        $nameEsc = $file.Name.Replace("[","``[").Replace("]","``]")
        $path = $file.DirectoryName
    
        New-Item -ItemType SymbolicLink -Path "$LinkPath\$name" -Value "$path\$nameEsc"
    }

    This one works like a charm.

    • Marked as answer by tiganita Saturday, April 4, 2020 12:46 PM
    Saturday, April 4, 2020 12:45 PM

All replies

  • You cannot use brackets in file names.  They will cause issues and are generally considered illegal in Windows.  Rename the files to use correct characters.


    \_(ツ)_/

    Friday, April 3, 2020 10:54 PM
  • This might help you somehow.

    Windows PowerShell Tip of the Week 06/08/2010


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    Friday, April 3, 2020 11:41 PM
  • Thank you @BOfH-666 That helped understand everything better.

    Still, the issue was a lot simpler. 

    Because of the -Recurse and the files sitting in subfolders of the $FilePath, the Path in the -Value parameter wasn't correct (missing the subfolder).
    I still had to replace the square brackets with two ticks and then it worked.

    $files = Get-ChildItem -Path "$FilePath" -Recurse -File -Exclude *.log,*.ps1 
    
    Foreach ($file in $files) {
        
        $name = $file.name
        $nameEsc = $file.Name.Replace("[","``[").Replace("]","``]")
        $path = $file.DirectoryName
    
        New-Item -ItemType SymbolicLink -Path "$LinkPath\$name" -Value "$path\$nameEsc"
    }

    This one works like a charm.

    • Marked as answer by tiganita Saturday, April 4, 2020 12:46 PM
    Saturday, April 4, 2020 12:45 PM