r/Python • • Sep 18 '20

Resource PowerShell vs Python Reference

https://blog.ironmansoftware.com/powershell-vs-python/
198 Upvotes

14 comments sorted by

19

u/flopana Sep 19 '20

Since you cna execute shell commands with python I sometimes use it as a shellscript on crack. Because you cna basically do everything that shellscript can and also have standard python plus the boat loads of libraries

5

u/LightShadow 3.13-dev in prod Sep 19 '20

I use invoke instead of Makefiles now.

There's also konch for fast loading an "environment" for scripting...and Xonsh which uses Python as a shell environment and scripting language.

7

u/shiftedcloud Sep 19 '20

ipython makes this even better.

0

u/[deleted] Sep 19 '20

[deleted]

6

u/shiftedcloud Sep 19 '20

It's super neat. Tab completion. Syntax highlighting. Multiline paste to a single, editable bulk line.

It's really nice when you're just trying to figure your way through a problem.

2

u/[deleted] Sep 19 '20

Also inbuilt help (through docstrings)

1

u/alcalde Nov 23 '20

Check out xonsh.

8

u/pepoluan Sep 19 '20

I am a Python programmer and also a PowerShell programmer... and everytime I have to write PowerShell programs I have to grit my teeth.

It's not that PowerShell is not capable; it's just that PowerShell is full of idiosyncrasies due to the fact that it's half a shell and half an API to .Net. Kinda parallel to iPython. Or Xonsh.

The programming pattern is all over the map and so inconsistent, I always cry inwardly wishing I can get back to Python.

That said, Posh developers are awesome for doing the heroic effort to bridge the gap; I have nothing but 👍🏼 for them.

1

u/ThePrankMonkey Sep 19 '20

Don't even get me started on the weird ways you need to write PS1 for working with Exchange or SharePoint.

6

u/WadeEffingWilson Sep 19 '20

Love this. I have a much stronger PS background but I'm using python more and more.

5

u/IvoryJam Sep 18 '20

This is pretty fun, sometimes I have to write in powershell and this is very helpful!

2

u/WadeEffingWilson Sep 19 '20 edited Sep 19 '20

A few essential things that are left out:

  • PS has switch/case conditional statements whereas python does not. You can approximate the functionality with if/else statements

PS example:

switch ($var) {
    case1 { $true_condition1 }
    case2 { $true_condition2 }
    default { $nothing_matched }
    }

Python example:

if var == case1:
    true_condition1
elif var ==  case2:
    true_condition2
else:
    nothing_matched  
  • PS doesn't have the slice vector notation capability like python (wish it did)

PS example:

# getting elements 0-5
@($array[0],$array[1],$array[2],$array[3],$array[4],$array[5])

Python example:

# getting elements 0-5
array[:5]  
  • PS's feature set can be extended by using inline C#

4

u/[deleted] Sep 19 '20

In Python, you can emulate switch cases more effectively than if/else statements using a dictionary acting as a dispatch table.

Because functions in Python are objects, they can be stored and called from anywhere an object could be.

Meaning I can do things like

table = { case_1: true_condition }

def true_condition(): print(“Hello World”)

2

u/WadeEffingWilson Sep 19 '20

Interesting! I never thought to use it that way. Thanks!

1

u/i5513 Sep 19 '20

But switch on powershell is like awk on Linux, It is not as simple like other switch statements in others program languages