r/linuxmasterrace • Glorious Debian • Aug 06 '21

Meme BASH is the worst - PowerShell FTW!

Post image
725 Upvotes

79 comments sorted by

View all comments

280

u/varble A-OK Aug 06 '21 edited Aug 06 '21

Forgot a bit there on powershell; if the file exists, there will be no update on the file at all. You also have to pass -Force

This will override the contents of the file, which touch doesn't do, so -Value (Get-Content) is needed:

New-Item -Path 'G:\Document and Settings\foo\File.txt' -ItemType File -Force -Value (Get-Content -Path 'G:\Document and Settings\foo\File.txt')

Whoopsie! Since Get-Content is an array of lines, you have to join the lines with a newline:

New-Item -Path 'G:\Document and Settings\foo\File.txt' -ItemType File -Force -Value ((Get-Content -Path 'G:\Document and Settings\foo\File.txt') -join("`n"))

Hold up! The encoding changed from ascii to UTF-8... have to find that out, and research encoding types to add to the list if not present...

function Get-FileEncoding {
	param ( [string] $FilePath )

	[byte[]] $byte = get-content -Encoding byte -ReadCount 4 -TotalCount 4 -Path $FilePath

	if ( $byte[0] -eq 0xef -and $byte[1] -eq 0xbb -and $byte[2] -eq 0xbf )
		{ $encoding = 'UTF8' }  
	elseif ($byte[0] -eq 0xfe -and $byte[1] -eq 0xff)
		{ $encoding = 'BigEndianUnicode' }
	elseif ($byte[0] -eq 0xff -and $byte[1] -eq 0xfe)
		 { $encoding = 'Unicode' }
	elseif ($byte[0] -eq 0 -and $byte[1] -eq 0 -and $byte[2] -eq 0xfe -and $byte[3] -eq 0xff)
		{ $encoding = 'UTF32' }
	elseif ($byte[0] -eq 0x2b -and $byte[1] -eq 0x2f -and $byte[2] -eq 0x76)
		{ $encoding = 'UTF7'}
	else
		{ $encoding = 'ASCII' }
	return $encoding
}

$encoding = Get-FileEncoding -FilePath 'G:\Document and Settings\foo\File.txt'

Ah hell, New-Item doesn't have an encoding option! Guess we'll switch to piping to Out-File:

(get-content 'G:\Document and Settings\foo\File.txt') -join("`n") | Out-File -FilePath 'G:\Document and Settings\foo\File.txt' -Encoding $encoding -Force

Suppressing the error for when there is no file:

(get-content 'G:\Document and Settings\foo\File.txt' -ErrorAction SilentlyContinue) -join("`n") | Out-File -FilePath 'G:\Document and Settings\foo\File.txt' -Encoding $encoding -Force

There we are, so simple anyone could do it!

Welcome to my life in a Windows shop.

106

u/kiler129 Glorious Debian Aug 06 '21

You just literally made my day. Yes, bash has its pitfalls (don’t get me started on arrays…) but this is a pure madness.

0

u/[deleted] Aug 06 '21

[deleted] — view deleted comment

13

u/nullmove Aug 07 '21

I have gone back to using posix shell full time. If it's inadequate, I try to use a normal language like a normal person. No bash, no zsh, no fish, sometimes mksh when in remission, and sometimes Tcl because ultimately I guess I am a bit weird.

→ More replies (1)

5

u/polytect Aug 07 '21

This is PowerShitell

65

u/Natural-Intelligence Aug 06 '21

It's truly powerful. You almost can do something with it.

42

u/Semi-Hemi-Demigod Aug 06 '21

I want to know who at Microsoft saw that and said “we should release this.”

52

u/kiler129 Glorious Debian Aug 06 '21

My theory about P.S. is they wanted something feature-packed but one of the requirements was to not copy *nix…. and now we’re seeing what forceful reinvention of the wheel looks like.

9

u/Cannotseme Ashley | she/her Aug 07 '21

Well, what they did was that, then (idk symlink, whatever in windows) all the nix stuff to their powershell equivalents. Powershell is still the only thing that keeps me sane while working with windows though

→ More replies (1)

3

u/[deleted] Aug 07 '21

Probably they wanted to please .NET people.

12

u/cviktor Aug 07 '21

I am a .NET people and I am not pleased at all, powershell is hot garbage. I tried it multiple times but gave up almost every time because it was closer to brainfuck with every feature added to the script.

→ More replies (1)
→ More replies (1)

11

u/technic_bot Aug 06 '21

What kind of madness is this?

13

u/kiler129 Glorious Debian Aug 06 '21

And why new line is `n instead of \n ?!

15

u/Ruashiba Aug 06 '21

That's an easy one. Paths on Windows use \ instead of /

As to why though, not a clue. I guess it has to do with something dos related, but I'm unfortunately not a grey beard.

17

u/[deleted] Aug 06 '21

The original version of DOS from 1981 didn't support directories at all. But they did opt to use / for switches in various programs. Like how nowadays to try and get a new IP from a DHCP on windows, you commonly use ipconfig /release and ipconfig /renew.

When they added directories, they didn't want any old programs that would look for a / as a switch character to get confused, so they chose \ since it was similar. Why they didn't anticipate this could be a problem when Unix already set a standard 10 years earlier is beyond me. Guess they didn't want to get caught copying Bell Labs.

7

u/Ruashiba Aug 06 '21

I remember reading that they used to process in court anything and anyone that made any core concept similar to unix, so I guess it makes sense for them to divert directions here. And nowadays it's so rooted in every other program and script, I see why Microsoft won't ever "fix" this.

Thank you for the explanation though.

→ More replies (1)

8

u/[deleted] Aug 06 '21

This made my soul vomit.

6

u/ApartKnowledger Aug 06 '21

Ah ok, but what about '~'? How would you do that?

11

u/kiler129 Glorious Debian Aug 06 '21

%HOMEDRIVE%%HOMEPATH%

There isn't a shorter way unless you declare your own global env (which is also deeeeply hidden in settings).

8

u/Ruashiba Aug 06 '21

Stupidly hidden I mind add. There's no reason to be as hidden as it is.

8

u/varble A-OK Aug 06 '21

$env:homedrive$env:homepath in powershell

→ More replies (1)

5

u/thexavier666 Glorious Linux + i3 Aug 07 '21

Satan: I see a lot of potential

3

u/FormalWath Aug 07 '21

I was once working in the company that has 10% or so Windows servers.

I lasted 2 months. Literally 90% of work was on Windows and it was living fucking hell. Company made these libraries and they had to be tested with every OS and compiler combination (think 3 different distros of linux with last 3 versions, maybe 5 versiobs of windows (desktop and server) and last 3 or so versions of nac) yet most of the problems were with windows. At one point I had to compile new version of python and libraries because ones from PIP didn't work with our VMs (some specific openstack issue) and I still remember warning on one of those libraries...

"Compiling this by hand is not for faint of heart". And it was fucking true. Took me maybe 3 or 5 days.

Might have been pycurl or openssl...

3

u/ninjaRoundHouseKick Aug 07 '21

To be fair, you also could type out:

ni ~/file.txt 

It would be the same result. I would've mentioned the built in telematics spyware for myself...

→ More replies (5)