1

I'm trying to write a Powershell script for work that will take the output of w32tm.exe \monitor and output everything into a text file. I'm trying the following code, however something is wrong since I'm able to create the text file, but nothing is being written in it:

#Take output of w32tm.exe and output into a plain text file
$file = "C:\Documents and Settings\a411882\My Documents\Scripts\timeScript.txt"
$executable = "w32tm.exe /monitor"
invoke-expression $executable | out-file -filepath $file

What am I doing wrong here? I'm new to Powershell so any advice would be greatly appreciated!

EDIT: I can get all of the data to be displayed on the console when I run this, however I want the data to be written on the text file.

EDIT 2: I managed to get everything to finally output. I wanted to try avoiding using the > operator to write out to the text file in hopes of learning a little more on the out-file cmdlet however doing a simple invoke-expression $executable > $file managed to get the job done. I still don't understand why the cmdlet wouldn't work properly.

1
  • just using invoke-expression $executable give you the expected output to console? Using your code in my session, changing just the path to my profile dir it works! Commented Jun 12, 2012 at 15:23

2 Answers 2

1

Perhaps Invoke-Expression isn't properly sending output to stdout? I wouldn't expect that, but stranger things have happened. Try running w32tm.exe on its own:

$file = "C:\Documents and Settings\a411882\My Documents\Scripts\timeScript.txt"
w32tm.exe /monitor | Out-File -FilePath $file
Sign up to request clarification or add additional context in comments.

Comments

0

You could use Start-Process which allows you to redirect also the error output:

Start-Process w32tm.exe -ArgumentList "/monitor" -Wait -RedirectStandardOutput "output.txt" -RedirectStandardError "error.txt"

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.