ALMOST "tail" Powershell effect works remotely

I'm looking for the Windows 7 equivalent of the "tail" command and thought I found it with this Powershell equivalent -

    C:\>powershell -command "& {Get-Content file.txt | Select-Object -last 100}"


If I use this in the CMD prompt on my own Windows 7 PC, it returns the information just fine. I can even add / add it to another file.

However, when I log in remotely to another PC (via openSSH) the command works, but it never returns me to the command line - it just hangs showing me the last 100 lines of the file. This means it won't work for the batch file I'm trying to edit for about 300 remote Windows 7 PCs.

Any ideas?


source to share

2 answers

After trying MANY suggestions found on the internet, FINALLY found one that works!

And the answer is inside the Batch file itself. My batch file to call this Powershell line was as follows:

    Powershell.exe -noprofile -executionpolicy Bypass C:\log\Tail.ps1


Again, works great if you are using it on the very PC you want to run / get information from. But not remotely. Finally, you just need to add " <nul " at the end of your call to Powershell in a batch file, just like

    Powershell.exe -noprofile -executionpolicy Bypass C:\log\Tail.ps1 <nul


What the other person wrote eventually makes sense: “My research has shown that PowerShell runs commands in a script specified via the -File switch, and then waits for additional PowerShell commands from standard input (my short experiments with the -Command switch demonstrated similar By redirecting stdin to nul, as soon as PowerShell finishes executing the script and "reads end of file" from stdin, PowerShell exits. "

Found here on this page - Powershell script gets stuck, not called when called from a batch file so credit really goes to @Gordon Smith



Since you are running the command with -command "..."

, according to the docs , you need to specify a flag -noexit

to stop powershell from exiting after executing the command.

powershell -command "& {Get-Content file.txt | Select-Object -last 100}" -noexit


When adding this to a batch file, you probably need -noprofile and -ninterinteractive, although for remote commands, you may need to create a process for better management and error handling. Also, if that doesn't work, the problem is probably related to how OpenSSH is handling something (it worked for me on a test server with a remote connection)



All Articles