0

My server is Windows Server. I would like to replicate the Unix tail command in Windows Server.

Unix Server: tail -f test.txt

PowerShell: Get-Content test.txt

How to execute in Windows Server?

Below command is not working:

powershell -File "Get-Content test.txt"

Error message:

Unable to execute program 'powershell -File "\"Get-Content...

Any idea?

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Sham
  • 1
  • 1
  • 2

3 Answers3

3

Get-Content is not a file; it is a cmdlet. The -file parameter to Powershell.exe instructs Powershell to read the file supplied and execute the commands in it, as a script.

You can pass commands directly to Powershell by using the -command parameter; the parameter can be a quoted string, which is interpreted as a Powershell command. You would therefore want to use

powershell -command "Get-Content test.txt"

in the simplest case.

Note that Powershell.exe must be in your system path; if it is not, you would need to supply the full path to powershell, e.g.,

C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe -command "Get-Content text.txt"

This question is very similar - perhaps essentially identical - to Unix tail equivalent command in Windows Powershell; I would recommend reading that question and its answers as well.

Additionally, exploring the help for Get-Content will provide useful information.

Community
  • 1
  • 1
Jeff Zeitlin
  • 9,773
  • 2
  • 21
  • 33
  • Thanks Jeff Zeitlin. Working fine after setting full path of **powershell.exe without quotes** `C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Command Get-Content test.txt` – Sham Jan 06 '17 at 06:57
0

Working fine after setting full path of powershell.exe and without any quotes

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Command Get-Content test.txt

Sham
  • 1
  • 1
  • 2
-1

Within a powershell window :

Get-Content test.txt

command returns :

hello world. i'm inside test.txt. bye.

Don Pedro
  • 41
  • 5
  • This does not answer the OP's question; he is trying to execute the Get-Content cmdlet by invoking Powershell externally, and getting an error. – Jeff Zeitlin Jan 05 '17 at 15:04