You can already test PowerShell in Ubuntu, Debian and CentOSrn
In the comments section of a recent post I found out that Windows PowerShell had been ported to Linux. Had no clue it was a thing.
Went looking and found this old article attempting to explain why they did it. Not remotely interested in giving up Bash for PowerShell, but I thought it was interesting enough to share. The article seems to be from 2016.
I have never been more tempted to check the NSFW box, but I'll leave it open for now unless a mod complains. :-D
How does this help though? If anything they would've helped themselves by porting more Linux commands to work natively in Windows. This move makes it easier for Windows admins and devs to switch to Linux. With the latest horrible moves in the Windows desktop space I can't believe they're trying to become the "RedHat of Windows".
If your os is windows and use ps, you can use ps on your linux vms as well. It prevents that you have to learn power and bourne. Such that it feels a bit more integrated. If you couldn't use ps, you had to use both shells which may lead to a migration to linux
Can someone explain to me why? The outputs are objects and that is cool for scripts, but the fact that every small thing is its own cmdlet is super annoying. I can do everything in Linux if I know 10 commands. In PS I would always have to look up everything.
the fact that every small thing is its own cmdlet is super annoying. I can do everything in Linux if I know 10 commands
That sounds more like a clash of cultures than a real problem. In Linux you need to know 10 options and possibly subcommands for each command. Naturally the same concept has different flags, and the same flag has different meanings in different commands. Is that really better?
If I recall the Verb-Noun idea is supposed to make it clear what is happening, take a look through stuff like the approved verbs for defining cmdlets. There's aliases and stuff for sure for example I think ls is an aliases for Get-ChildItem in PowerShell.
It's supposed to make it so you don't necessarily need to look things up, need to do something to an item? Well you can Copy, Remove, Rename, Move etc, and while yeah that's a super basic example that you know the equivalent linux commands for, the concept is supposed to apply everywhere. Now, whether or not people follow the guidelines is probably another story.
I don't really hate shell scripting, feel like they all have their place, complex stuff though is nicer in straight PowerShell than bash IMO, but I'm fine using either.
I'll take your word for it. I could never wrap my head around PowerShell back when I still had a Windows install. Whenever I could, I would use either the DOS prompt or WSL/Ubuntu. I may not be great at Bash or DOS but at least I'm not having to resort to cargo culting to do anything. Probably a sign I'm getting old.
Random question for everyone from a bit of a noob. When I'm using Powershell (PS) in windows I can start to type the name of a built in command or one I have added to PATH and then press tab to auto complete the command. That part works the same in my Linux terminal.
What I can also do after I have typed that command into PS is start to type a file name that exists in the directory that PS is working in and then press tab to auto complete or cycle through the files that match and it even formats the name of the file correctly (meaning if it has a space in the name it will wrap the name in quotes so that it is understood by the commands they are fed to). This auto completing of file names even works on files that were created after the PS window was opened. This functionality doesn't seem to exist by default in any distro I have used. Is it possible to do this in the Linux terminal?
Although I have done some distro hopping, most of them have ultimately been Ubuntu based. Currently running Kubuntu.
No grep though as far as I could find... There was a similar cmdlet IIRC, but it was extremely limited and didn't work well (this was years ago though)
The idea with powershell isn't to be a text parser - so grep doesn't really work. When you pass things through pipes, it's a full object with multiple properties, and those you can filter with either simple expressions like select-object [-property] or with more complex expressions: https://4sysops.com/archives/add-a-calculated-property-with-select-object-in-powershell/
It’s really useful too. You can either pipe in text or for example Get-ChildItem a directory of files and it will parse them all. As usual it returns a helpful object with properties like line number.
Its a completely different shell, not just another terminal emulator.
Its more readable, and its syntax is less arcane than bash.
For example, a script to get the first line of a file and its lenght in bash is:
#!/bin/bash
if [ "$#" -ne 1 ]; then
echo "Usage: $0 filename"
exit 1
fi
filename="$1"
if [ ! -r "$filename" ]; then
echo "File '$filename' does not exist or is not readable."
exit 1
fi
read -r first_line < "$filename"
echo "First line: $first_line"
length=${#first_line}
echo "Length of first line: $length"
There is so much I hate about this, like using a semicolon after the if condition, and ending it in fi.
Versus the powershell version:
param (
[Parameter(Mandatory = $true)]
[string]$FilePath
)
if (-not (Test-Path -Path $FilePath)) {
Write-Error "File '$FilePath' does not exist."
exit 1
}
try {
$firstLine = Get-Content -Path $FilePath -TotalCount 1
}
catch {
Write-Error "Could not read from file '$FilePath'."
exit 1
}
Write-Output "First line: $firstLine"
$lineLength = $firstLine.Length
Write-Output "Length of first line: $lineLength"
I can maybe chime in since I've used both. Basic operations like if statements, arithmetic and loops are a lot closer to what you'd expect on PS. The barrier to programming in bash vs PS is IMO a bit higher because bash heavily uses symbols for everything. This does make PS way more verbose but more easy to wrap your head around it when unfamiliar with the syntax.
I prefer bash but for anything bigger than 5 lines I prefer proper scripting language like python or js and making an alias for "node path/to/js/script.js" and using execSync("program param1 param2") to run shell commands.
Long story short, I prefer bash because it's built in and I know it better than PS, I expect PS guys to feel the same way.
I work on both Linux and windows machines for some software projects. It's nice to be able to write powershell helper scripts and have it work on both.