Powershell - Useful Commands

The past few months I’ve been fiddling with Powershell and leveraging it at work. For instance, with a recent roll-out of an in-place Windows 10 upgrade (it was a money thing) needs a lot of garbage cleaned up (Store, Xbox, ect)…

The command that was listed in an old TS Windows 10 “cleanup” video Get-AppxPackage -allusers | Remove-AppxPackage doesn’t exactly work correctly in 1709 (Fall Creator’s Update). In previous releases this command would remove ALL the apps for ALL users, but now it’s per user.

You might think “run as admin” and feel free to drop the -allusers flag. But that actually won’t do anything for the user at hand. In this case, the command needs to be run as that user… BUT admin rights are still needed.

The first option could be make the user an admin while needing this, but that isn’t very efficient. Fortunately Windows handles user rights in a strange way, so pipe-ing in Get-Credential is the sauce to make this magic work.

Thus, to remove the garbage like the store for a non-admin is Get-AppxPackage | Remove-AppxPackage | Get-Credential and use an admin account.


Now for something a little more niche… Hyper-V Replication can be a somewhat unreliable, so it’s a good idea to keep an eye on it. But checking through Hyper-V Manager (especially with a large number of VM’s) is tedious and annoying.

Measure-VMReplication is command that solves this, but it doesn’t show all of the useful data outright (not to mention the -ComputerName flag doesn’t seem to work the way other commands usually do. So using Invoke-Command is what I needed to be able to run this remotely.

Even though Measure-VMReplication doesn’t show all of the useful data, it does have it available in its containing objects. So I wrote a function that would make it more useful.

function Check-VMReplication {
param ($virtsrv)
$tstsrv = Test-Connection $virtsrv -Count 1 -Quiet
if ($tstsrv -eq “True”){
Invoke-Command -ComputerName $virtsrv -ScriptBlock {
Measure-VMReplication | % {
Write-Host $.Name $.ReplicationState $.ReplicationHealth “with” $.ReplicationErrors “errors.” $.SuccessfulReplicationCount “succeeded, last replica” $.LastReplicationTime “with” ("{0:N0}" -f ($_.PendingReplicationSize/1MB)) “MB pending”
}
}
}
else { Write-Error -Category DeviceError -Message $virtsrv “could not be reached via PING.”}
}

For those who don’t know much about powershell functions, they’re a lot like custom commands… Or how alias functions in BASH.

The function sends a ping, and runs Measure-VMReplication and displays info for each VM such as errors and pending data. If the ping fails, it displays an error message. Just run Check-VMReplication HyperVHost


Building on my last function, I threw together a loop.

function Monitor-VMReplication {
param ($mvirtsrv)
while ($true) {cls; Get-Date -DisplayHint DateTime; Check-VMReplication $mvirtsrv; sleep 10}
}

Monitor-VMReplication HyperVHost it’ll clear the text, show the time, and run the check command I wrote.


I’m working on a few other functions that I think would be useful but I’m still working out the details of how I want to go about writing it. One of said functions is to search an OU in Active Directory for computer names to put into other commands’ -ComputerName flag.

7 Likes

I am all for using PowerShell to learn PowerShell, too.

First, always run

update-help

Then, PowerShell has the Get-Command cmdlet which accepts wildcard searches.

Get-Command New*

Finally, with your known command, -Full or -Examples

Get-Help New-PSSession -Examples

Great post! If you have to work in Windows or .NET, PowerShell is a shining tool. Very powerful and flexible. PowerShell Scripting in a Month of Lunches just came out. It builds off of the Toolmaking in a Month of Lunches.

If anyone is looking for a great start, the first book in the series is PowerShell in a Month of Lunches and it is really good, too.

2 Likes

Tip for the thread: Powershell ISE is your friend. Use it. If you’re still hanging out on Windows 7, upgrade Powershell first, and then Powershell ISE will be your friend.

2 Likes

Powerful commands? Theres only one. The exit button.

1 Like

sfc /scannow

Would remove a lot of threads on various forums all over the Internet^^

1 Like

woh… a windows centric post. :)~

2 Likes

I would recommend VS Code with the PowerShell extension. Microsoft is phasing out ISE if I recall.

2 Likes

http://www.nooooooooooooooo.com/

4 Likes

Lmao I hate bearing terrible news

2 Likes

I know. BLASPHEMY!

Not gonna lie, the only time i use powershell is when I need to download a file as a restricted user.

Invoke-WebRequest has saved me so many times.

1 Like

With all of the stuff that I’ve been working on lately, there’s been a lot of editing of text files in my workflow… And having to open stuff in an external editor got very annoying very quickly. I’m partial to vim when I’m using *nix OS’s, so I got curious if there was anything similar for powershell.

Turns out there are two ways about getting vim to work under powershell, but only one of them was actually useful (without all sorts of extra weird nonsense).


Starting with the less optimal solution… If you have Linux for Windows installed (I installed Ubuntu to try this), just open powershell and run bash -c “vim /mnt/c/filename.txt”. You can call nano, emacs, or whatever with this. But there’s a problem with this… I’m dealing with both local and remote files, so I wasn’t exactly interested in writing a function to convert Windows file paths for bash to understand.

Turns out there’s a Windows port of Vim… there’s a download for this on sourceforge, but I opted to install it through chocolatey. Once it’s installed,
‘C:\Program Files (x86)\Vim\Vim80\vim.exe’ C:\Folder\filename.txt aaannd it works, but that’s a lot to type…To get around this, just run
Set-Alias vim “C:\Program Files (x86)\Vim\Vim80\vim.exe”. Then it’s as easy as vim C:\Folder\filename.txt


As an afterthought with bash -c, I started thinking about what else could be done with calling bash through powershell… ping.exe is a little light on features, and Test-Connection can in some cases be worse.

I went into $PROFILE and added

function ping {
param ($args)
bash -c “ping $args”
}

reloaded powershell, and ping google.com called for bash instead of ping.exe… Only catch is that if any additional arguments are used, all of the parameter needs to be put in quotes…
ping ‘google.com -D’

Without too much thought this seems silly, I could just open the bash prompt. Well, by weird extension the output from these can be put into variables. Which means that they can be use inside powershell scripts… Which might prove to be useful at some point.

I realize this is necromancy, but creating a new thread seemed silly

4 Likes

Thanks for your contribution. Powershell is its own beast.

I hear its better now that they finally took a fucking hint from bash and have SSH functionality now.

When did they add this?

currently I would just use WSL or putty.

has been a thing for at least a year now. not a lot of word of mouth going on about what you can do in bash on windows so if it has been there from the start or recent addition i cant tell you.

1 Like

Huh, last I tried (not long ago) ssh wasn’t there, now:

Yeah, since MS is switching themselves to cloud services, this in includes Linux containers, so that’s why they have Linux subsystem as well as ssh in ps now. Wouldn’t make sense if they didn’t have a way for you to manage your stuff and instead be forced to use putty.

The reason they never included it before was because under Redmond they didn’t recognize Linux and retored with “why would you need to manage your windows server headless when you have”.

So that’s why there will buddy buddy with Linux now.

1 Like

I read recently that when some stuff changed over at Microsoft, the teams in that area (cloud, PowerShell, etc. like you said), were told give the customers what they want, and wsl, opensource PowerShell, ssh, etc. came to life.

I knew ssh was coming, didn’t realise it was there. ssh from wsl works just as well. Its also nice with powershell in that you can pass anything back and fourth from WSL and powershell like @Jebedia47’s example.

The one I do like, and have used frequently is Get-FileHash

Get-FileHash <filepath> -Algorithm SHA512 | Format-List