1

I have a PowerShell script which copies some file from one folder to another folder and I also have code to create a log file and add the steps that are performed by the script to the log file.

Below is the code -

[CmdletBinding()]
Param (
    [string]$destRoot = "C:\Program Files\Demo",
    [string]$BackUpFolder = "C:\Program Files\Backup"  
)

$scriptPath = $PSScriptRoot
$logFilePath = Join-path $scriptPath "FileDeployment.log"

# If log file exists, then clear its contents 
if (Test-Path $logFilePath) { clear-content -Path $logFilePath } 

# It displays the date and time of execution of powershell script in log file.
$log = "Date Of Testing: {0} " -f (Get-Date)
$logString = "Process Started." 
add-content -Path $logFilePath -Value $log -Force 
add-content -Path $logFilePath -Value $logString -Force 
add-content -Path $logFilePath -Value "`n" -Force

# Code to take back of programs folder
$ProgramFilesBackUp = "$BackUpFolder\BackupPrograms"
$ProgramsFiles = "$destRoot\Programs\*"

Copy-Item $ProgramsFiles $ProgramFilesBackUp -Recurse -Force -Verbose 

The -Verbose displays all the files names along with its path getting copied from Programs folder to BackupPrograms folder but it fails to add that output to my log files which is shown on command shell. I tried something like this but it overwrites the previous output present in the log file.

Copy-Item $ProgramsFiles $ProgramFilesBackUp -Recurse -Force -Verbose  *> $logFilePath

I also tried the below code as well by copying the output generated by verbose to a new file and then try to append the content of the file to my log file, here it copied the content but not in the proper format.

$NewText= "$PSScriptRoot\s.txt"
Copy-Item $ProgramsFiles $ProgramFilesBackUp -Recurse -Force -Verbose  *> $NewText
Get-Content $NewText | Out-File $logFilePath -Verbose -Append 

Below is the format the way in which it copied the content.

enter image description here

henrycarteruk
  • 12,708
  • 2
  • 36
  • 40
SRP
  • 999
  • 4
  • 21
  • 39

1 Answers1

5

You should read and thoroughly understand Get-Help about_Redirection, which identifies all of the various output streams, and discusses how to redirect them. In this case, since you've indicated interest in the -Verbose stream, you want to redirect stream #4 to the standard output, which is done by appending 4>&1 to the command, before actually doing the redirect > foolog.txt or >> foolog.txt.

NOTE: This may not work for Powershell 2.0; it is documented for 3.0 and later.

Jeff Zeitlin
  • 9,773
  • 2
  • 21
  • 33
  • Correct, PSv2 *only* supports error redirection (`2>&1`). PSv3 introduced `warning`, `verbose`, and `debug` stream redirection. PSv5 introduced `information` stream redirection (`6>&1`) – Maximilian Burszley May 15 '18 at 14:15
  • @TheIncorrigible1 - I couldn't have said that for certain; the MS Docs site (linked to on the `Get-Help about_Redirection`) only goes back to 3.0 now - they've apparently completely dropped support for 2.0. – Jeff Zeitlin May 15 '18 at 14:18
  • Gotcha, just adding more context and history. I believe v3 added the wildcard redirection as well. – Maximilian Burszley May 15 '18 at 14:19