Showing posts with label Windows. Show all posts
Showing posts with label Windows. Show all posts

Wednesday, January 25, 2017

Uninstall Windows Service using PowerShell

You can get a list of services using the Get-Service cmdlet, for example, to retrieve a list of all running services you could run:

Get-Service | Where-Object { $_.Status -eq "Running" }

You could even retrieve a specific service and start/stop it:

$service = Get-Service DemoService
$service.Start()
$service.Stop()

However, there is nothing to remove the service on this object.

We can also use GetWmiObject to do the same things:

$service = Get-WmiObject Win32_Service -Filter "name='DemoService'"
$service.StartService()
$service.StopService()

But this WMI object actually also exposes a delete functionality, which will mark the service to be deleted.

$service = Get-WmiObject Win32_Service -Filter "name='DemoService'"
$service.Delete()

If there are resources that are being held onto, it will only be deleted once they're freed up - whether it's the service that needs to gracefully shutdown still, or maybe the services manager snap-in (services.msc) has an open properties window open (I think that's happened to me before), and closing it then puts the delete through.

Friday, January 9, 2015

Removing NodeJS folder on Windows

When playing around with demo projects on NodeJS, and then later trying to throw them away and delete the folders, you've most likely encountered the error: The file name is too long. We'll ignore the question of if it's too long, how it got created in the first place...
To illustrate it, create a folder that will cause a long file path. I've had this problem with grunt-contrib-imagemin, so lets install that package too:
1
cd \tmp
2
mkdir this-is-probably-a-really-long-folder-name
3
cd this-is-probably-a-really-long-folder-name
4
mkdir yeah-it-probably-is
5
cd yeah-it-probably-is
6
npm init
7
npm install grunt-contrib-imagemin -D
Now, if you try and delete this folder, you will get the pesky The file name is too long error:
1
cd \tmp
2
rmdir /s /q this-is-probably-a-really-long-folder-name
3
this-is-probably-a-really-long-folder-name\YEAH-I~1\NODE_M~1\GRUNT-~1\NODE_M~1\imagemin\NODE_M~1\IMF332~1\NODE_M~1\gifsicle\NODE_M~1\BIN-BU~1\NODE_M~1\download\NODE_M~1\DECOMP~2\NODE_M~1\TAR-ST~1\NODE_M~1\END-OF~1\NODE_M~1\once\NODE_M~1\wrappy\package.json - The file name is too long.
4
...
Le sigh.
If you manually try and change into that directory, at some point you'll also get an issue where you can't even change into it because the filename is too long. But wait, if you look at the above, everything is using short file names except the first folder. Hmm, what if we try the short file name?
1
dir /x
2
rmdir /s /q THIS-I~1
It works! Well, it did for me at least.

Tuesday, December 30, 2014

Finding the hostname for an IP address on Windows

When trying to investigate networking issues, most people know about ping and nslookup. I discovered today that there's also a utility called nbtstat to get some NetBIOS info, including the hostname and MAC address from an IP address.

nbtstat -a IPADDRESS

Wednesday, February 1, 2012

Symbolic links and hard links in Windows

A lot of people coming from the Linux world would probably have at some point used symbolic links or soft links, and hard links.

Windows has had shortcuts for a long time, but these are actually files that are used to point to other files, they aren't actually entries in the filesystem that merely reference other files, which is what the links are, meaning that shortcuts take up more space.

A lesser known fact about Windows is that it actually also has symbolic links, hard links, and also with Windows NTFS has the concept of a junction point, which is a hard link for a directory. The differences between them is discussed quite nicely in the post "Windows File Junctions, Symbolic Links and Hard Links".

It also appears that the capability has been around for a while, going back to Windows 2000. With Windows 7 and probably most newer Windows versions these days, there's also a nice command line tool mklink to help with this.

Open up a command line, type mklink /? and then try it out for yourself in a test area, playing with creating new files, linking to them, deleting the original files, seeing how the links are affected, etc.

Disclaimer: If you delete something that wasn't just a pointer to a file, and was the actual file, that's your own fault, don't come looking for me :D

Thursday, June 9, 2011

Starting Windows Services Remotely

Did you know you can use the sc command to start services remotely?

sc \\SERVERNAME start SERVICENAME

e.g.

sc \\test-server query AppFabricCachingService
sc \\test-server start AppFabricCachingService