1

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?

4
  • 2
    The command should fail with a different error. How exactly are you invoking it? Commented Jan 5, 2017 at 14:50
  • 1
    powershell -File "myScript.ps1" vs powershell -Command "Get-Content test.txt" Commented Jan 5, 2017 at 14:55
  • 2
    @briantist That too (and also add -Tail for following the file), but his error message suggests that for some reason his entire statement is interpreted as a single command. Commented Jan 5, 2017 at 14:58
  • 2
    @AnsgarWiechers agreed; I wonder if this is being invoked through some other software Commented Jan 5, 2017 at 14:59

3 Answers 3

4

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.

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

1 Comment

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
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

Comments

-1

Within a powershell window :

Get-Content test.txt

command returns :

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

1 Comment

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.

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.