Is it worth learning Powershell?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
OM3
Posts: 32
Joined: 17 Mar 2014 08:43

Is it worth learning Powershell?

#1 Post by OM3 » 20 Oct 2023 06:47

I've always stuck to DOS for small things I need to get done.
Just wondering if it's worth investing time getting to know Powershell?
Will it benefit me more in anyway?
Just wondering

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Is it worth learning Powershell?

#2 Post by Aacini » 20 Oct 2023 17:43

First point: PowerShell is more powerful than Batch files. There are tasks that can not be completed in a Batch file, but in PowerShell. However, such tasks are rare. The vast majority of daily management tasks can be completed with a Batch file.

That said, IMHO PowerShell is difficult to learn and use. The syntax is different than most other scripting languages and frequently appear errors that you can't understand even if you read they several times! You need a specialized support to successfully pass the first steps in learning PowerShell. Of course, this is just my opinion, but if you are interested in this topic, you can read a lot of opinions about (against) PS from other people at the comments that appear below this S.O. answer (like this one).

In my opinion, if you want to learn another, more powerful, scripting language in order to replace Batch files, I suggest you to choose VBScript or JScript. VBScript is somewhat "simpler" and JScript a little more "technicall", but both uses standard syntax that will not give you unpleasant surprises, like PS!

Antonio

Batcher
Posts: 74
Joined: 16 Apr 2009 10:36

Re: Is it worth learning Powershell?

#3 Post by Batcher » 20 Oct 2023 20:20

Yes. It will do.

Batch is good and easy enough for tasks including 1) string and text operations, 2) file and folder operations, 3) GUI operations which could be done by built-in CLI exe commands.

PowerShell is helpful for other tasks, especially the ones lack of existing commands.

DOSadnie
Posts: 143
Joined: 21 Jul 2022 15:12
Location: Coding Kindergarten

Re: Is it worth learning Powershell?

#4 Post by DOSadnie » 22 Oct 2023 09:10

OM3 wrote:
20 Oct 2023 06:47
I've always stuck to DOS for small things I need to get done.
[...]
Me too; and I just a small time code tweaker. But time after time it turns out that BAT scripting is insufficient

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Is it worth learning Powershell?

#5 Post by aGerman » 23 Oct 2023 15:58

tl;dr Yes it's worth learning PS.

I think we all have some reasons why we decided to do Batch scripting. For some of us just historical reasons like because we grew up with Batch, we are used to write Batch scripts, we are more experienced with Batch than with other scripting languages, we know how to handle obstacles like early variable expansion and other pitfalls ... Batch still has its raison d'être e.g. for simple automation tasks where quite some specialized tools exist (mainly thinking of file system work like ROBOCOPY, ICACLS and friends). Or even just because it's fun trying to get beyond the obvious limits of this language. IMO Batch is still a good choice for pragmatic people who just want to get the task done (preferably unattended) and who don't care much about visual and audible effects. It's been developed for a simple textual interface with originally only allowing a single keystroke for user input (SET /P is an extension that came with NT first). Even things that alredy existed once, like the CHOICE tool, were removed in-between times e.g on XP. That emphasizes what Microsoft thought about the aim of Batch scripting.

Serious arguments against Batch (IMHO):
- Batch is one of the most limited languages ever. Its origin is literally just a batch/list of single command lines. It never really evolved to loose this characteristic. This makes that Batch is not a mature scripting language. Thasks for which no CLI tool is available are often just not to be done. There's no possibility to directly access .NET classes or the Win32 API (or even 3rd party libraries).
- Batch is one of the most difficult languages. This is often underestimated because you may successfully resolve supposedly difficult tasks very quickly, at least if you found a CLI tool dedicated to do the task. But: The definition and use of variables may have side effects. The order of instructions in a command line may have side effects. Using or ommitting spaces in a command line may have side effects. Apart from UTF-8 (to only some extend) there is no Unicode support; instead the CMD typically interprets the script code in another charset than the editors where it was written, which may have side effects. Variables are environment variables that are directly passed on to child processes. All of these peculiarities (which would be beyond the scope to describe in more detail here and which often do not follow any specific rules) have to be known and kept in mind when writing batch code, which makes it almost impossible to write scripts without bugs.
- The behavior of command lines may differ depending on whether they are executed in a Batch script or in a CMD prompt.
- Math is a nightmare. Basic arithmetic and bitwise operations are supported. That's it. Not even floating point numbers, only 32-Bit signed integers. That's why 2 / 3 = 0.
- Many data types, such as arrays and datetime types that are intrinsic in other languages are just not there.
- Batch doesn't support the asynchronous execution of parallel threads/jobs.
- Error handling is difficult. You need to rely on the errorlevel being set properly by commands/tools and you have to check the errorlevel after every command, at least in theory. There's no such things like exceptions or exception handling.
- Batch allows to use GOTO to jump to labels. That's for compatibility reasons and because it lacks certain loop types (like WHILE). This tempts to write unreadable and unmaintainable spaghetti code (although subroutines are supported since NT was introduced).
- The limitations of the language lead to exploits of every bug and of all kind of undefined behavior. That's a serious form of "Hyrum's Law" because people (including me) have been deliberately done investigations to find usable exploits. However, such things shouldn't ever be a base for code that you want users to rely on.
- The fact that all kind of bugs are already intentionally exploited prevents Microsoft from fixing them as this would instantly break too many scripts worldwide. (Refer to the console/terminal core team https://github.com/microsoft/terminal/b ... ksa.md#cmd These statements can be extended to the whole toolset typically used in a Batch script.) Thus, Batch keeps being an error prone language with umpteen bugs that are often difficult to recognize, especially if they occur only under certain circumstances.

In brief, a few of the arguments in favor of PowerShell (without any claim to completeness):
- Here, too, there are ready-made CmdLets for special tasks to prevent you from constantly reinventing the wheel. But beyond that, everything that is possible with .NET is also possible in PowerShell. This includes access to the platform API if required. In other words, you don't necessarily need compiled tools for certain tasks. Everything can be done in the script code.
- Threading, in the form of asynchronous jobs, is supported.
- Unicode is supported. (BTW: The developments in PowerShell, Windows Terminal and even Notepad indicate that we can probably expect UTF-8 as a quasi-standard for text-based interfaces on Windows in the medium term.)
- PowerShell is under active development. PowerShell Core is an open source project and is available not only for Windows, but also for Linux distros and macOS. Scripts can therefore also be used across platforms if they don't invoke the platform API.

However, I also have some caveats regarding PowerShell:
- Just like Batch, the typical PS syntax isn't comparable with any other language. It may include a few elements of C-like languages. However, experiences in Batch or Linux shell scripting are only little helpful.
- PS's pipe syntax encourages writing excessively long lines of code. Some people seem to find it fascinating to write everything in just one line. Similar to the overuse of GOTO in Batch, this has the downside of losing readability.
- By far not everything is already wrapped into CmdLets. Therefore, it is beneficial to be familiar with .Net or to gain experience with it. If you want to do so, C#.Net should be preferred since custom classes need to be written in C# code anyway.
- As soon as you need to go beyond predefined CmdLets, you also have to go beyond the typical PS syntax. E.g. if you need to call methods of .Net classes. At a closer look, PS scripting is often not as clean as it should be expected.
- It's annoying that Windows PowerShell (v5.1) still requires scripts to get invoked from the context menu. This seems to be gone with PowerShell Core installed (at least on my Win 11). However, Windows still ships with only 5.1.
- PS takes longer to start up because it has to load all of its dependencies first.

Steffen

miskox
Posts: 630
Joined: 28 Jun 2010 03:46

Re: Is it worth learning Powershell?

#6 Post by miskox » 24 Oct 2023 10:32

I will just add this: I think PS is very good for (some?) admin repetive tasks (why I wrote 'some?'? - because I don't take care of servers but this is based on what I read so I don't have such experiences).

I use PS almost daily for some special task: I have (my customer really) 150+ Excel files. And I automated the export of only *one* column from each Excel file to a .txt file and based on that I do lots of work (comparisons etc.) in pure batch.

Yes, I think it is good to learn PS.

Saso

koko
Posts: 38
Joined: 13 Oct 2016 00:40

Re: Is it worth learning Powershell?

#7 Post by koko » 02 Nov 2023 23:37

It stuns me the hurdles to just running Powershell scripts. It's easier to run both a binary or batch script that launches Powershell than PS per se, which seems counter-intuitive if their goal was to encourage it as a replacement but I'm probably veering off-topic.

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Is it worth learning Powershell?

#8 Post by ShadowThief » 03 Nov 2023 10:14

miskox wrote:
24 Oct 2023 10:32
I will just add this: I think PS is very good for (some?) admin repetive tasks (why I wrote 'some?'? - because I don't take care of servers but this is based on what I read so I don't have such experiences).

I use PS almost daily for some special task: I have (my customer really) 150+ Excel files. And I automated the export of only *one* column from each Excel file to a .txt file and based on that I do lots of work (comparisons etc.) in pure batch.

Yes, I think it is good to learn PS.

Saso
I'm surprised you aren't using VBScript for that

miskox
Posts: 630
Joined: 28 Jun 2010 03:46

Re: Is it worth learning Powershell?

#9 Post by miskox » 03 Nov 2023 10:47

ShadowThief wrote:
03 Nov 2023 10:14
I'm surprised you aren't using VBScript for that
Easy answer: because I don't 'speak' VBScript.

I must say that PS (ImportExcel cmd-let) does the job but I am always open for new (faster?) options.

Update: did some searching: there are ome VBScripts. I only have to export *one* column to csv.

Saso

MarkAJA62
Posts: 1
Joined: 07 Jan 2024 08:04

Re: Is it worth learning Powershell?

#10 Post by MarkAJA62 » 07 Jan 2024 08:22

ShadowThief
I always try to avoid VBScript because:
Not supported with Netscape.
Only supported with Internet Explorer and clones using Trident.
Internet Explorer no longer supported.


Or did you mean Visual Basic as in Visual Basic 5 and not VBScript?
I believe that VBScript is now only supported in Microsoft Office or is that VBA.
Website at https://learn.microsoft.com/en-us/windo ... d-features
says VBScript is being removed from the operating system as from October 2023.

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Is it worth learning Powershell?

#11 Post by ShadowThief » 07 Jan 2024 11:34

I'm talking specifically about the language that uses the cscript and wscript interpreters. https://en.wikipedia.org/wiki/VBScript It's a scripting language for ActiveX that's modeled on Visual Basic and that I've used multiple times as standalone replacements for VBA macros that I've written. Wikipedia says that it technically can be used in webpages, but I don't think I've seen that happen in the last 25 years.

Office products use VBA, which is a dialect of Visual Basic that adds functionality for Office products.

Post Reply