When to Reach for Another Language
PowerShell is at its best as glue: files, Windows, other programs and quick-run applications; a script that grows past a few hundred lines, needs a web page or a phone app, has to run fast over millions of items or runs where PowerShell can't is a sign to look at C#, Python or JavaScript, and the languages work together, passing JSON, rather than taking sides.
Journeyman · PowerShell · Updated
After this lesson you can
- Choose between keeping a job in PowerShell and moving it to C#, Python or JavaScript, from the signs the job shows
- Run a program written in another language from PowerShell and turn the JSON it prints into objects
Key takeaways
- PowerShell is at its best as glue: files and folders, Windows, other programs, reports and quick-run applications.
- Signs a script wants to be a program: it's too big to change safely, it needs a web page, phone app or many users, it's too slow, or it must run where PowerShell can't.
- C# is .NET's own language, Python leads in data and AI, and JavaScript runs every web page. Each is best at something.
- Languages work together: PowerShell runs another language's program and turns its JSON into objects with ConvertFrom-Json.
Your backup script started as twenty lines. Now it's six hundred, two friends want it on a web page, and someone at work says "real programs are written in C#". Another friend says "everyone uses Python now". Who's right? Usually neither, and sometimes both. The Terminal and the Mouse: Two Ways In promised options, not sides, and the same goes for languages: each one is the best tool for some jobs. This lesson is an honest guide to when PowerShell is the right tool, the signs that a script wants to become a program in another language, and how languages share the work. Developers make this choice on every project, and knowing it is part of the job.
What PowerShell is best at
PowerShell is glue: it joins things that weren't built to work together. It's at its best when the job is:
- files and folders: sorting, renaming, backing up, cleaning;
- Windows itself: settings, services, logs, scheduled jobs, many computers at once;
- running other programs, such as
git,dotnetandnpm, and checking how they went; - turning data (CSV, JSON, a web API) into a report;
- a quick-run application for you or your team, as A Script Is an Application showed.
There's nothing to compile, a few lines do a lot, and every Windows computer already has it.
Signs a script wants to be a program
None of these is a rule. One sign is a reason to think, not to rewrite.
- It's too big to change safely. Past a few hundred lines, even with Pester tests, you avoid touching parts of it.
- Other people need it as an app: a web page, a phone app, or many users at once.
- It's slow. A loop over millions of rows can take minutes in PowerShell, where a compiled program takes a fraction of that.
- It has to run where PowerShell can't: in a web browser, on a phone, on a tiny device.
- A team works on it every day and wants mistakes caught before it runs. That's static typing: each variable's type is fixed and checked before the program starts. PowerShell finds most mistakes only when the line with them runs.
Which language for what
- C# is .NET's own language. The
[Math]and[IO.Path]you called from All of .NET at Your Fingertips work the same there, and PowerShell itself is written in C#. It makes a compiled program, checked and built into a program file before it runs. It's used for web apps and APIs, Windows apps and games. Coming from PowerShell, it's the shortest step. - Python leads in data analysis, science and AI, and is a favorite for automation on Linux. Its code is known for being clear to read, and there's a package for nearly everything.
- JavaScript runs every web page in every browser, and with Node.js it runs servers and command-line tools too.
Whichever you choose next, you start ahead: variables, loops, functions, objects, error handling and tests work in all of them.
Working together: options, not sides
Real projects mix languages: a website in JavaScript, its server in C#, a report in Python, and PowerShell scripts that build, test and ship all three, as in The Scripts Every Project Has. They pass work to each other as text both sides understand, usually JSON (JSON: ConvertFrom-Json and ConvertTo-Json).
Here's a small example. A club's Python script counts its sign-ups and prints JSON, and PowerShell takes it from there. These lines make the practice folder, the sign-up sheet and the Python script:
New-Item -ItemType Directory -Force -Path "$HOME\terminal-practice\club" | Out-Null
Set-Location "$HOME\terminal-practice\club"
Set-Content -Path signups.csv -Value 'Name,Activity', 'Ava,Pottery', 'Ben,Board games', 'Chloe,Hiking', 'Dev,Hiking', 'Elena,Board games', 'Felix,Hiking'
Set-Content -Path tally.py -Value @'
# tally.py: counts the sign-ups for each activity and prints them as JSON.
import csv, json
counts = {}
with open('signups.csv', newline='') as f:
for row in csv.DictReader(f):
counts[row['Activity']] = counts.get(row['Activity'], 0) + 1
print(json.dumps([{'Activity': a, 'Count': c} for a, c in counts.items()]))
'@
If Python is installed (python.org; on a Mac or Linux the command is often python3), run it. On its own it prints one line of JSON; piped into ConvertFrom-Json, the line becomes objects that PowerShell can sort:
python .\tally.py
python .\tally.py | ConvertFrom-Json | Sort-Object -Property Count -Descending
[{"Activity": "Pottery", "Count": 1}, {"Activity": "Board games", "Count": 2}, {"Activity": "Hiking", "Count": 3}]
Activity Count
-------- -----
Hiking 3
Board games 2
Pottery 1
Python counted, and PowerShell sorted, and could filter it or make a report, as with any other objects. It works the other way too: a C# or Python program can run a PowerShell script. Neither language had to win.
Check yourself
2 questions to try on the lesson page.
So what
PowerShell doesn't have to do everything, and it doesn't have to step aside either: knowing what each language is best at lets you pick the right tool and make them work together.