0

Let's say I have a folder with name "Folder [MyGitHub]" and it has one subfolder with name "1"

When I try the following powershell,

$sourcePath = "D:\Folder [MyGitHub]"
[array]$buildDirectories = Get-ChildItem $sourcePath | Sort-Object Name
Write-Output $BuildDirectories[0]

The exception is:

Cannot index into a null array. At line:1 char:1    + Write-Output $BuildDirectories[0]    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException    + FullyQualifiedErrorId : NullArray

I have tried escaping with backtick (`), backslash, single and double quotes, but the issue was not resolved. The code is working if [] are not present in the folder name string - example Folder (MyGitHub).

K. B.
  • 3,342
  • 3
  • 19
  • 32
  • 2
    `$buildDirectories = @(Get-ChildItem -LiteralPath $sourcePath | Sort-Object Name)`. To escape, use backticks: ```"D:\Folder ``[MyGitHub``]"``` or ```'D:\Folder `[MyGitHub`]'``` (notice that `"`-quoted strings need _double_ backticks) – Mathias R. Jessen Feb 28 '23 at 11:15
  • `Get-ChildItem 'D:\Folder ``[MyGitHub``]'` or `Get-ChildItem -LiteralPath 'D:\Folder [MyGitHub]'` – iRon Feb 28 '23 at 11:20

0 Answers0