Why I fell in love with Powershell?
Introduction
I know I’m late to the show but in my defense I’ve been using Linux ever since I had a laptop . It’s been quite a great experience learning about Windows, and Powershell has only made things easier for me. I cursed myself for doing some of the tasks in vbscript and later looking at the one-liners in powershell. At this point, I have installed powershell even on linux( It’s not the same as Windows probably due to lack of COM and WMI but it’s great for simple tasks).
How to install powershell on linux?
If you’re on a debian distribution like I am, then following commands should do the trick:
wget -q https://packages.microsoft.com/config/ubuntu/18.04/packages-microsoft-prod.deb
sudo dpkg -i packages-microsoft-prod.deb
sudo apt update
sudo apt install powershell
pwsh
Have fun playing around with it. If you’re having any issues installing or you use a different distribution , go to https://docs.microsoft.com/en-us/powershell/scripting/install/installing-powershell-core-on-linux
You could also run a docker image, personally I use this when I need Powershell remoting. Here’s the link : https://hub.docker.com/r/quickbreach/powershell-ntlm/ .
Features:
Let me start with the one I loved the most.
- All the other shells in the world are text-based . For instance, we use grep or other text parsing tools just to have a look at the part we’re interested in. Does it work? Yes. Is it reliable? Maybe. Is it painful? Definitely. Powershell is object based and that means more reliability and less pain. If I wanted to know all the running process id with bash, this is how I’d do it :
ps aux | cut -c10,11,12,13,14,15
Here’s the text parsing I was grumbling about. I would have to know by trial and error method which columns to pick. Let’s see how it’s done in powershell .
$p = Get-Process
$p.Id
How awesome is that?
2. There are plenty of useful cmdlets available but let’s say you have a task at hand and cmdlet isn’t available for that task.
Addressing the elephant in the room : .NET framework. Powershell is built upon .NET so it’s only natural that it provides access to the framework. .NET framework is huge and core part of Windows OS. If you want .NET Core sdk available on Linux, here’s the link: https://dotnet.microsoft.com/download/linux-package-manager/ubuntu18-04/sdk-current .
Working with .NET framework has been made as easy as possible. You just need to know the name of the class ,property and method. Here’s an instance:
It’s that easy to create .NET objects (by using New-Object) ,call their methods( by using dot operator) and look at their properties( by using ::) .
3. Powershell not only gives acess to .NET framework but also COM( Componet Object Model) . Here’s an example to disable firewall:
$firewall = New-Object -com HNetCfg.FwMgr
$firewall.LocalPolicy.CurrentProfile.FirewallEnabled = $false
4. Powershell Remoting: the one that kept me up at 3.00 AM without a yawn. Pretty sure anyone who’s in security would read about it line by line to find how to abuse this feature, like I did. To be fair, this feature requires a blog post of its own. For sake of sanity and length of this post , I am painfully skipping over how to enable PS-remoting .
If you’re in an environment where PS-remoting is enabled on all systems, you just need to know IP address/name of the computer you want access to.
Enter-PSSession <Name of the computer/IP address>
You can now type commands as if you were locally present on the system with one exception. If you need access to other network resources/computers, you cannot do it remotely, this is known as the double-hop problem. The problem being that you cannot pass your credentials implicitly on the second hop. To tackle that, one of the most used way is CredSSP authentication mechanism. To know more solutions, read this : https://blogs.technet.microsoft.com/ashleymcglone/2016/08/30/powershell-remoting-kerberos-double-hop-solved-securely/ .
Other Features I’m painfully skipping over/ Will make another post about:
- Infamous Internet-Enabled cmdlets and features for web-scraping
- Powershell’s way of accessing Windows Registry
- Event-logging capabilities
- Ease of access to WMI
Conclusion
These were some of the most amazing features which attracted me. Feel free to ask a question or leave a suggestion in the comments section. Also, let me know if there’s some other amazing feature I forgot about.