Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I wrote a PS1 script that will count how many processes (in this case MS Project) are running on a Citrix farm.

The code it's:

$variable=(query farm /process | where { $_.Contains("WINPROJ") }  | measure ).count
echo $variable

If I execute the PS1 from powershell, it works flawlessly but If I invoke it from cmd (for schedule it) using powershell.exe -file "C:\foo\countProcessProject.ps1" or powershell.exe -c "C:\foo\countProcessProject.ps1" the script will stop until I press a button multiple times, outputting the variable.

I've compared environment variables on CMD and PS execution and are almost the same.


share|improve this question
up vote 1 down vote accepted

You might be able to force it to return using the -NonInteractive argument with powershell:

PowerShell.exe -File "C:\foo\countProcessProject.ps1" -NonInteractive

While I have no insightful explanation as to why this would be necessary, here is how I would work around this issue:

Without PowerShell:

If you insist on using the archaeological gem of query farm, this can be done using cmd alone - no need to complicate a simple task:

query farm /process |find /i "winproj" /c

This will output the number "winproj" processes much in the same way as your current powershell invocation statement

Using just PowerShell:

If you are keen on moving on to a more PowerShell-centric solution, you can use the PS Snapins included in the offical XenApp PowerShell SDK from Citrix:

Add-PSSnapin Citrix.XenApp.Commands
$FarmProcs = Get-XAServer -OnlineOnly | Get-XASessionProcess
$MSProjProcs = $FarmProcs | Where {ProcessName -match "winproj"} | Measure-Object
return $MSProjProcs.Count
share|improve this answer
It works, and I love the use of the powershell cmdlets (I didn't know about the XA PS SDK... now on bookmarks for future scripts). – vfondevilla Jan 21 '14 at 8:42
The XenApp cmdlets are not super versatile, but they work and they ease the pain of wrapping commandline management. Glad to hear it works :) – Mathias R. Jessen Jan 21 '14 at 8:53

I'll in first place try to put this :

PowerShell.exe -file "C:\foo\countProcessProject.ps1"

In a batch file and then create a Schedule Task that runs this batch which will run your Powershell script.

Assuming you've not already tried this.

share|improve this answer
I tried again calling the script via bat file & task scheduler, same result. Until I press any keyboard button it keeps freezed – vfondevilla Jan 17 '14 at 15:31

Does it work when you use the start /b command to spawn powershell inside the cmd console.

start /b /wait Powershell.exe -file "C:\foo\countProcessProject.ps1"

Can you confirm you are not clicking anywhere on the screen during the script enabling the click pause as described here.

share|improve this answer
Yeah, tried this too and it doesn't works. I disabled the quick edition mode on cmd by default – vfondevilla Jan 20 '14 at 11:17

I'm still learning PowerScript and don't have the points to comment above, but have been looking at this and have some questions. Hope you will indulge me.

First I can create a simple test file and run it from cmd like this:

$v = (dir | Where-Object { $"test") } | measure).count
echo $v

I run this in cmd with:

powershell -file test.ps1

It takes a moment but then returns 1 because my file is test.ps1

What is the 'query farm /process' ? Is this an external command? And what does it return? I think the pipe following it is expecting an array, is it not? I'm wondering what happens to errors when you run from cmd. I do know that a 2> command line suffix null will cleanly get rid of any error.

share|improve this answer
So I had a little time to think about this, and if you get the chance please run the script I wrote above as a test. It will be interesting if it too has the same trouble, because it does not have that trouble here. – Eliptical view Jan 18 '14 at 2:52
query farm /process it's a Citrix command for list all XenApp's farm processes. The script you've wrote works without problems from CMD and also from task scheduler – vfondevilla Jan 20 '14 at 10:53
humm? Then there appears to be something strange about query farm /process, like it's stopping and asking for standard input or something, or a batch file, or checking some env variable, or something strange. ?? – Eliptical view Jan 20 '14 at 13:37
It seems, but in a Powershell windows the .PS1 runs without problem. I'll try on another approach – vfondevilla Jan 20 '14 at 14:33

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.