1

Below code is in batch file as i'm running powershell script with batch file. but doesn't give any output.

I am trying to use the tee code written for a bat file but am having trouble implementing it in my code.

Is there a way to have the output of display in the console window as well as put it into the text file?

xcopy "%~dp0*" "C:\windows\Temp\Check" /q /s /e /y /i
PowerShell.exe -ExecutionPolicy Bypass -File %~dp0Check.ps1 "dir | tee C:\temp\output.txt"
4
  • See Tee-Object - PowerShell - SS64.com
    – DavidPostill
    Commented Nov 24, 2022 at 12:58
  • @DavidPostill shared suggestion is for powershell. but the code pasted is for batch file. As i'm running powershell script with batch file. Commented Nov 24, 2022 at 14:17
  • 1
    a) don't run PowerShell in Batch, just run everything in PowerShell. b) you can use tee-object in your check.ps1
    – SimonS
    Commented Nov 24, 2022 at 20:01
  • Just curious. Why you are doing it this way when PS and or Robocopy can do this natively? You are mixing syntax improperly and overcomplicating this.
    – postanote
    Commented Nov 24, 2022 at 20:04

1 Answer 1

1

As per my comment, you are mixing syntax improperly.

%~dp0

...is not any PowerShell will understand. The PowerShell equivalent of that is...

$PSScriptRoot

Also, You are trying to run batch/cmd syntax in your -File switch. PS has no idea what that is. That is not a file name.

You then have a Get-ChildIem alias on the same line and it's not terminated, so, again, that is not proper PowerShell code. It this,

PowerShell.exe -ExecutionPolicy Bypass -File %~dp0Check.ps1;"dir | tee D:\temp\output.txt"

...but again, this is not valid PS code, because of the mixed syntax.

The PowerShell equivalent of %~dp0is...

$PSScriptRoot

PS has tons of special/reserved characters, and %, is an alias for ForEach-Object, and that ```~``, has no meaning in PS.

Get-Alias -Name '%'
# Results
<#
CommandType Name                Version Source
----------- ----                ------- ------
Alias       % -> ForEach-Object  
#>

Get-Alias -Name '~'
# Results
<#
Get-Alias : This command cannot find a matching alias because an alias with the name '~' does not exist.
#>

You can just do this in PS.

Copy-Item -Path $Source -Destination $Target -Recurse -Force | 
Tee-Object -FilePath 'C:\temp\output.txt'

... or again, just use Robocopy.exe.

MSDocs: robocopy

Copies file data from one location to another.

https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/robocopy

https://www.delftstack.com/howto/powershell/powershell-robocopy/

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .