0

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 ""
1
  • This will never work due to syntax issues. Commented Jan 2, 2024 at 13:03

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.