views:

1070

answers:

5

&& is notoriously hard to search for on google, but the best I've found is this article which says to use -and.

Unfortunately it doesn't give any more information, and I can't find out what I'm supposed to do with -and (again, a notoriously hard thing to search for)

The context I'm trying to use it in is "execute cmd1, and if successful, execute cmd2", basically this:

csc /t:exe /out:a.exe SomeFile.cs && a.exe

This should be an easy few rep points to someone who knows, thanks!

+1  A: 

It depends on the context, but here's an example of "-and" in action:

get-childitem | where-object { $_.Name.StartsWith("f") -and $_.Length -gt 10kb }

So that's getting all the files bigger than 10kb in a directory whose filename starts with "f".

Matt Hamilton
A: 

PowerShell has -and and -or logical operators. Are the operators not working the way you expect them to? If so, you can post the expression here.

Mohit Chakraborty
+4  A: 

In CMD, '&&' means "execute command 1, and if it succeeds, execute command 2". I have used it for things like:

build && run_tests

In PowerShell, the closest thing you can do is:

(build) -and (run_tests)

It has the same logic, but the output text from the commands is lost. Maybe it is good enough for you, though.

EDIT

If you're doing this in a script, you will probably be better off separating the statements, like this:

build
if ($?) {
    run_tests
}
Jay Bazuzi
I hadn't surrounded the actions with brackets. Once I did that, it worked (but unfortunately just echoed True when it was finished). It seems rather ridiculous that they would remove (well, cripple) such basic functionality
Orion Edwards
Orion Edwards
Jay Bazuzi
RE: "ridiculous that they would remove" - I don't like to think of PowerShell as "CMD with the stupid parts removed". http://is.gd/k92B
Jay Bazuzi
ok, perhaps not "remove", but "fail to implement"...
Orion Edwards
If you ask the PowerShell team, they'll say "To is to choose", I bet.
Jay Bazuzi
I don't like to think of PowerShell as "CMD with the stupid parts removed". I like to think of it as "Bash without any of the useful bits".
Pod
Even adding the parens it doesn't act as a short cutting conditional logic and operator, it still runs both commands even if the first returns false...
joshperry
@joshperry: that doesn't match my experience. I just repeated a simple test `($false) -and (x)` vs. `($true) -and (x)`. One reports an error, one does not.
Jay Bazuzi
+3  A: 

&& and || were on the list of things to implement (still are) but did not pop up as the next most useful thing to add. The reason is that we have -AND and -OR. If you think it is important, please file a suggestion on Connect and we'll consider it for V3.

Experiment! Enjoy! Engage!

Jeffrey Snover [MSFT] Windows Management Partner Architect Visit the Windows PowerShell Team blog at: http://blogs.msdn.com/PowerShell Visit the Windows PowerShell ScriptCenter at: http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx

Jeffrey Snover - MSFT
I'm signed up on Connect, and have nominated myself for powershell, but I can't figure out how to place a suggestion. The Connect website is really complex and confusing :-(
Orion Edwards
A: 

-and -or -lt -gt -le -ge ...

no offense to the powershell team, but were you guys on crack when you decided to use those ridiculous things and not the standard && || < > <= >=.

In a shell the characters `|`, `>` and `<` are already taken for input and output redirection. Look at bash and others. You'll see the same trend with operators.
Joey