0

What would be the equivalent statement of CMD's:

dir && cd ..

in Powershell?

I tried:

dir -and cd ..

but it throws error:

Get-ChildItem : A parameter cannot be found that matches parameter name 'and'.

At line:1 char:5

+ dir -and (cd ..)

+ CategoryInfo : InvalidArgument: (:) [Get-ChildItem], ParameterBindingException

+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell .Commands.GetChildItemCommand

Annie
  • 3,090
  • 9
  • 36
  • 74
  • Vote here: [GitHub](https://github.com/PowerShell/PowerShell/issues/3241) and here: [UserVoice](https://windowsserver.uservoice.com/forums/301869-powershell/suggestions/11087898-implement-the-and-operators-that-bash-has) – pilau Feb 07 '19 at 10:24

2 Answers2

4

There is not a direct equivalent in PowerShell of cmd.exe && which means "only execute right-hand side if the left-hand side succeeds." However you could write a short function to do the equivalent:

function IfTrue([ScriptBlock] $testExpression, [ScriptBlock] $runExpression) {
  if ( & $testExpression ) { & $runExpression }
}

For example:

IfTrue { get-childitem "fileThatExists.txt" -ea SilentlyContinue } { "File exists..." }

If you want for the $testExpression to produce output, the IfTrue function can be written as follows:

function IfTrue([ScriptBlock] $testExpression, [ScriptBlock] $runExpression) {
  & $testExpression
  if ( $? ) { & $runExpression }
}

Bill

Bill_Stewart
  • 22,916
  • 4
  • 51
  • 62
  • Bill, I tried `IfTrue { get-childitem dir -ea SilentlyContinue } { cd .. }` but nothing displayed (no directory listing)! Any ideas? – Annie Apr 16 '13 at 13:45
  • Do you have a file system item called 'dir'? 'dir' is an alias for 'get-childitem' in PowerShell. – Bill_Stewart Apr 16 '13 at 14:15
  • I am afraid not. Honestly, I am new to PowerShell. Would you please explain your answer to get `dir` and `cd ..` working in single statement? Thanks. – Annie Apr 16 '13 at 15:04
  • I tried with 'IfTrue { dir -ea SilentlyContinue } { cd .. }'. The `cd ..` seems to work, but `dir` is not listing directory. I even removed `-ea SilentlyContinue`, but still no luck. – Annie Apr 16 '13 at 15:25
  • I updated my answer (see above) to include a new version of the IfTrue function. The second version of the function produces output. – Bill_Stewart Apr 16 '13 at 15:30
2

How about this?

dir; if ($?) {cd ..}

Running get-help about_automatic_variables | more explains:

$? Contains the execution status of the last operation. It contains TRUE if the last operation succeeded and FALSE if it failed.

In PS, dir is just an alias for get-ChildItem; cd likewise for Set-Location.

edit: Same question here, including an answer straight from the horse's mouth.

Community
  • 1
  • 1
noam
  • 1,914
  • 2
  • 20
  • 26
  • Noam, thanks for the answer. This method works fine! OTOTH, is there any reason (as in conflicting with PS' foundation) that there is no support for batch operations? – Annie Apr 16 '13 at 15:27
  • Not enough upvotes [here](https://connect.microsoft.com/PowerShell/feedback/details/778798/implement-the-and-operators-that-bash-has)? – noam Apr 16 '13 at 15:38