How to add Double quotes to Array elements in Powershell console?

60 views Asked by At

I need to display all elements of the $FileNames array in Powershell console. I am iterating in a For loop but i cant print double quotes to all Array index elements on the console. Please help.

Actual Result: Dropped file:- "$FileNames[$i]" successfully into the folder

Expected Result with Double quotes(for every filename in the array): Dropped file:- "NewFile1.txt" successfully into the folder... and so on.

$FileNames = @(Get-ChildItem $TestFolder\*.txt | Select-Object -ExpandProperty Name) 
    
        For($i=0;$i -lt $FileNames.count;$i++)
        {    
            if($FileNames[$i] -eq $fileName)
            {
                $filePathNew = $TestFolder + $FileNames[$i]
    
    
                Copy-Item -Path $filePathNew -Destination $RemoteURLFolder\$FolderName -Recurse 
                Write-Host "=> Dropped file:- "'"$FileNames[$i]"'" successfully into the folder ""
2

There are 2 answers

0
user459872 On

You can use:

Write-Host "=> Dropped file:- `"$($FileNames[$i])`" successfully into the folder"

Changes made:

  • To escape the double quotes you have to use `.
  • To evaluate the complete expression($FileNames[$i]), You can warp it in a $().

See also

0
Theo On

Another (quite elegant) way would be to use the -f Format operator

Write-Host ('=> Dropped file:- "{0}" successfully into the folder'-f $FileNames[$i])