Category Archives: microsoft

getting a web page with basic authentication using powershell

pont d'amour Getting a web page using PowerShell is pretty easy using Invoke-WebRequest. Getting a web page which is protected using basic authentication isn’t that much harder, but it took me a while to find out how to do that exactly as my initial searches didn’t turn up the right answers.

The trick is that you have to pass in a credential object when you make the call:

$result = Invoke-WebRequest -Credential $credential

That $credential object is something you have to create. If you don’t pass in an object, you’ll get a prompt btw, which might also be handy.
Creating the object is done like this:

$user = "john.doe"
$password = ConvertTo-SecureString "a_password" -AsPlaintext -Force
$credential = New-Object PSCredential($user, $password)

The PSCredential object is created and the username and a secure password string is passed into the constructor.

Putting the username and password in your script like this is a bad idea in a lot of cases, so you should really consider if this what you want to do. You can also use the Get-Credential cmdlet to ask the user for the username and password instead.
That way you keep this sensitive data out of your script and make it more resilient to change as well.
This is all it takes to ask for the credentials and set them into a variable you use later on:

$credential = Get-Credential

erase free space on an SSD drive

DeleteAs you probably know (because you’re reading this post) Windows doesn’t really destroy a file when it is deleted. It merely removes the references in the file table so you don’t see it in on the file system anymore. But the file is still intact sitting there on your hard drive until some other file is written over it.
If you want to truly erase those files on a HDD drive you can use a number of tools to erase the free space on the drive. What these tools do is simply overwrite all free space with random data and thus effectively overwriting and destroying those deleted files still sitting intact in your free disk space.
Free tools that can do this are the command line Secure Delete tool from System Internals and the handy CCleaner (see the tools menu).

For an SSD drive however overwriting the free space with random data is bad for your drive. SSD’s have a limited number of times you can write data to their blocks. Using a random data overwrite tool, which can even end up do this multiple times, is just a bad idea.

Luckily everything is built into your SSD drive to do this automatically. It’s called the TRIM function and from Windows 7 on this is activated by default so normally you don’t have to do a thing to take advantage of this secure-delete feature.

However if you are like me then you want to be sure if this feature is activated.
On Windows, you can do this by opening a command prompt and entering the following command:

fsutil behavior query disabledeletenotify

If you get the following, your TRIM command is already active on Windows:

NTFS DisableDeleteNotify = 0

If this would return a value of 1, you can activate the TRIM function with this command:

fsutil behavior set DisableDeleteNotify 0

You can find more detailed instructions and information about activating the TRIM function here:
How to check if TRIM is enabled on your SSD and how to enable it
Why SSD TRIM support is so important and how to enable it

And then there’s always Google of course.

Photo by Delete, cc-licensed.

how to move an unreachable window on windows 7, 8 or 10

arrowsOn the “good” old XP this required some trickery and knowledge of window specific shortcuts, but on more recent version of Windows this has become really easy to do.

So if you run into a situation where an application’s window is outside your visible area, because you disconnected a second screen for example and the app doesn’t automatically snap to the only screen left, simply do this:

  • Keep the Windows key pressed and hit the cursor key left or right.

Your window will simply snap back to your current screen and all is well.
Using it with the up & down arrow will maximize or minimize the active window. Always handy to know those shortcuts if you have both hands on the keyboard anyway.

Photo by Dean Hochman, cc-licensed.

mstsc.exe crashes on Windows 8 (and how to fix it)

Physical View of the Network

When using MSTSC (Microsoft Terminal Server Client aka RDP or Remote Desktop on Windows) to dial into a local HyperV virtual machine on my laptop it often happened that MSTSC crashed on me. When reconnecting afterwards, the VM was running just fine. So it seemed to be an issue on my local machine and not the VM. I assumed it had something to do with HyperV and RDP and didn’t look into it any further (I didn’t need it that much after all).

The exception code I got in my Event Viewer for the crash was 0xc0000005 on ntdll.dll.

But then I kept having this problem so often when connecting to a physical machine in the network it annoyed me enough to Google for a possible solution.
One post pointed me in the right direction (which I can’t find anymore, sorry about that), being that it had something to do with sharing of local resources. In my case I recently activated the option to transfer sound between the remote and my local machine so I could use Skype & Linq.

Turns out that was indeed the culprit. Whenever my remote machine made a sound, my RDP client crashed. Bummer!
Turning off sound transferring to my host machine made the crashes go away. Sweet!

So if you also run into this, try turning off some of those local resources.

It still sucks if you need sound of course, but in my case, I can live with the silence.

Photo by Robin Gist, cc-licensed.

chocolatey package update quick reference


Chocolatey rocks when it comes to updating a bunch of installed software from the command line. If you’re not doing that often however it can be hard to remember exactly what commands you can use to do that quickly. So here’s a little run-down on the most helpful commands when you are updating your system.

First you might want to check what’s installed on your machine.
You can get the list of the local package Chocolatey installed like this:

chocolatey list -localonly

or in short:

clist -localonly

To check if any package have updates available, we can run the update all statement, but not quite for real yet. By adding the -whatif switch, Chocolatey only pretends to update:

chocolatey upgrade all -whatif


cup all -whatif

Ready to update all packages at once? Nice. So let’s disable those confirmation prompts while we’re at it too by adding the -y switch.

chocolatey upgrade all -y


cup all -y

Edited 2017-02-26 : replaced deprecated update command with the new Chocolatey 1.0 upgrade command.

Photo by elidr, cc-licensed.

why an open sourced .NET framework is huge


I was already excited about the recent ASP.NET vNext developments. Things like the fact that you can get the ASP.NET source code on Github, that it’s completely FOSS and that it’s disconnected from the rest of the .NET stack are just plain awesome.

A huge step for ASP.NET vNext is that you don’t need Visual Studio to write software with it. You can use your favourite text editor like Vim,┬áSublime, Emacs or whatever you like, together with a number of open source command line tools.

A second huge thing is that ASP.NET 5 can now run cross-platform using Mono on Linux and Mac. Not only can you use your development tools of choice to write and build your C# code, you can also do it on the OS of your choice. .NET everywhere. Think about it. *mind blown*

Yesterday however, things got even sweeter as Microsoft is now releasing more of the v5 .NET Framework as open source. This means more and easier cross-platform development and Mono compatibility (as the source can be easily integrated in Mono) for .NET code.

On top of that there is now a new Visual Studio Community edition of Visual Studio available for free. This is equal to the Pro version, so you can ditch those crippled free “Express” versions and write code in the tools you’re used to professionally. I love this one. I’ve messed around with SharpDevelop and the Express version, but if you’re used to the “real thing” it feels like having to work with your hands tied behind your back.

As if this wasn’t enough there’s a bunch of other cool improvements too, like getting only the .NET framework components you need for your project and pull them in using NuGet. Scott Hanselman sums them up nicely.

So if you’ve always wanted to check out .NET or C# but didn’t want to because you had to run it on Windows and in Visual Studio, there’s nothing holding you back any more. For .NET developers this is great. It gives us more freedom than ever without having to learn a new language and framework. For people hacking away on OS X and Ubuntu with Ruby, Python etc. because they want to use FOSS, this is an opportunity to dip into the wealth of .NET resources out there and try something new.

The strategy is clear. They want everyone to use the .NET framework, they want everybody to run that code on Azure (even if you’re not using .NET) and they see that making it open is the only way to get there. Great times are ahead.

how to update chocolatey

I use Chocolatey all the time to quickly install software on Windows machines. At some point Chocolatey itself also gets an upgrade, which just happened recently and then I can never remember how to get Chocolatey itself upgraded.

It’s in the documentation somewhere I’m sure, but since Chocolatey is about easily installing and upgrading Windows software, it was bound to work “recursively”.

So here’s how you do that from a Windows shell prompt, for (my) future reference:

    c:\> chocolatey upgrade chocolatey