How To Output A List Of All Files In A Directory Using PowerShell

October 28, 2014

PowerShell Logo

The Problem

I was recently tasked with sifting through years of stuff that had built up on one of the Windows servers at work and removing that which was no longer required. As a developer, this largely meant clearing out the local instance of SQL Server (tables, views, stored procs, functions, SSIS packages and jobs), but also there were a lot of batch (.bat) files and SQL (.sql) files knocking about.

These files needed cataloguing before I archived any of them as I knew that some were still in use. SQL Server wasn’t a problem as there are various system stored procedures to return a list of various entities, but how was I going to get a list of files that were scattered and buried in the ageing directory structure?

I tried using the Windows Explorer search function, but it was so hit and miss that I couldn’t rely upon it.

I thought maybe I could knock up an ASP.Net page to trawl through the folders and give me a list. Sure I could do this (I recall doing it with Classic ASP many moons ago), but then I remembered about PowerShell.

PowerShell

I’d only recently heard of PowerShell and seen some script and was impressed with what could be achieved, but I hadn’t had a real reason to use it. Basically, PowerShell is a powerful scripting tool that sits under the Windows GUI and allows the user to perform all sorts of magical tasks. I won’t go into detail here, so take a look at this: What can I do with PowerShell? Surely it could perform some basic file filtering and output the list. I wasn’t wrong.

Get To The Point

This is all you need to do:

Get-ChildItem C:\ -Recurse | Where {$_.extension -eq “.bat”} | ForEach-Object { $_.FullName } > C:\File.txt

Here’s how to paste script into PowerShell.

Break It Down

I’m not going to pretend that I know exactly what the switches do, but here’s a quick breakdown of the script:

  • The script is broken up by the pipes (|)
  • Get-ChildItem C:\ returns all files within the root of C:. The drive letter can be changed
  • -Recurse loops through all of the subdirectories C: and returns the files in those subdirectories too. How simple is that? I love it!
  • Where {$_.extension -eq “.bat”} only returns files with a .bat file extension. You can omit this if you want to return all file types
  • ForEach-Object { $_.FullName } returns the full path of each file, including the drive letter
  • > C:\File.txt chucks the results into a text file with a name and location of your choice. You can omit this to display the output within the PowerShell window instead

Bish, bash, bosh, job done!

 

Photo Credit: Joe Olson via Compfight cc

How To Paste Text Into PowerShell

October 28, 2014

PowerShell LogoSo you’ve found a PowerShell script online that you want to use or maybe you’ve saved a previously used script to a text file. All you want to do is copy the script and paste it into PowerShell, but there’s no Paste option in the context menu and CTRL+V doesn’t work.

There’s a nice easy solution to this problem:

  1. Open PowerShell
  2. Click on the PowerShell icon in the top left of the PowerShell window
  3. Expand Edit and select Paste

There are some other useful tools hidden away up there too.

 

Photo Credit: Joe Olson via Compfight cc

300 Beers, The First Five Beers

May 13, 2014

Way back in November 2013 I started my journey through the book 300 Beers To Try Before You Die! by Roger Protz. I haven’t posted since then, but it doesn’t mean I haven’t been working (well, you know, drinking).

One thing that I decided early on was not to give a score to any of the beers. Scoring something based on how much you enjoyed it is so subjective and, furthermore, leaves little or no room for higher scores if you think you found your perfect ten or 100% beer before you’ve tried all the other beers in the world first. Instead I opted to write whether I’d drink it again or not. This will be of limited use to you unless you find yourself liking most of the beers I’d drink again. The beauty is in the eye of the beholder!

300 Beers #1 - Fuller's London Pride

300 Beers #1 – Fuller’s London Pride (England)

Bottle blurb: “London Pride has a smooth, distinctive malty base, with a rich balance of well developed hop flavours from the Target, Challenger & Northdown varieties and our own unique yeast in the brew. An impressively complex beer. The UK’s favourite premium ale.”

Earthy, clear golden brown. An ale with a solid base. The branding normally puts me off buying this because it doesn’t scream ‘rustic pub in the countryside’, but branding aside, I’d drink this again.


300 Beers #2 - Ringwood Old Thumper

300 Beers #2 – Ringwood Old Thumper (England)

Bottle blurb: “Old Thumper delivers a deep brown strong ale with a spicy fruity hop aroma and a warming malty finish. The distinctive taste has made it a champion Beer of Britain.”

Rich brown in colour. Light spicy taste, not earthy. There’s something nice about this which prevents a lasting bitter aftertaste. I’d drink this again.


300 Beers #3 - Robinson's Old Tom

300 Beers #3 – Robinson’s Old Tom (England)

Bottle blurb: “Old Tom is a legend amongst beers. First created by the Robinson family in 1899. It is a wonderfully warming dark strong ale, with aromas of dark fruit and a palate booming with ripe malt and hops, slowly followed by a deep port wine finish. Not surprisingly Old Tom has won almost every brewing accolade including being voted The World’s Best Ale.”

Very dark brown with little head. I looks much like flat cola. It has a strong flavour that I’m not experienced enough to identify. They’re right about the port finish though. It’s nice enough, but it’s strong, so I probably wouldn’t choose to drink it.


300 Beers #4 - Weihenstephan Hefe Weissbier

300 Beers #4 – Weihenstephan Hefe Weissbier (Germany)

Bottle blurb: “First established in 1040, Weihenstephan is the world’s oldest brewery. It’s Hefeweissbier is the authentic unfiltered version of Bavarian wheat beer – cloudy, yeasty and refreshing.”

I read a little about this one before drinking it and can pick out the banana and clove flavours. Perhaps it’s not as easy-going on the taste buds as Hoegaarden, but I quickly got into the flavour once I had an understanding of what the flavours were. I wonder if I had chilled the beer too much. Worth noting that the 300 Beers book is quoted on the back of the bottle. I’d drink this again.


300 Beers #5 - Kirin Ichiban

300 Beers #5 – Kirin Ichiban (Japan)

Bottle blurb: “Ichiban Shibori is a process by which beer is made from a single first pressing of the finest ingredients, giving you the sweetest, most flavoursome beer every time. Only Kirin Ichiban is made with such uncompromising standards, for a purer, superior taste.”

Certainly a very crisp and refreshing beer. The book tells me that there is a “lilting lemon grass…aroma”. I can’t smell it, but I think I can taste it. I’m not detecting any strong flavours, though I can tell that it’s a strong beer. If I could only have one drink at the end of a day of hard labour, this would definitely be one of my options. As it turned out, the photos that I took of this beer were all blurred as a result of some shakey-handed photography, so I had to buy another bottle. Shame.

300 Beers To Try Before You Die!

November 29, 2013

I had my 40th birthday quite recently. It’s just a number, but it’s also a bit of a milestone for reflection. It’s a time for contemplation:

  • How well have I lived my life so far?
  • How should I live my life in the coming years?

It also marks the day when I received the book ‘300 Beers To Try Before You Die!’ by Roger Protz.

300 Beers To Try Before You Die!


300 Beers To Try Before You Die!Before I die?

Panic stations. I’ve got to get to work on this pronto!

OK, so maybe I’m not so concerned about my death, provided it’s due to natural causes and that it takes place in the latter part of this century. However, this book has piqued my interest. I’ve enjoyed drinking, what I call, ‘real ale’ for some years and, whilst I’m certainly no expert, I have developed a taste for what works and what doesn’t work for me. As a result, I’ve got my favourites and that is that. I occasionally try a new ale, but as a general rule, I know what I like and I stick with what I like.

What’s so special about this book, then?

Well, it offers me an achievable challenge: 300 beers at one per week is less than six years. I can do that. Better still, many of the beers are of British origin and should be fairly easy to obtain. Even better, quite a few of them are available in my local supermarkets or off licences. All this means that I can knock a few off the list within a few weeks. Then the real challenge starts. The European beers, the North and South American beers, the beers that are no longer in production (if there are any)…

So here’s what I will do:

  • Drink all 300 beers in no particular order, except those that are readily available to me will probably come first for an easy win.
  • Drink them when I feel like it. I won’t be aiming for one every week or any other kind of fixed schedule.
  • Take a photo of each beer.
  • I’ll take drinking notes. I’ve never done this before and my sense of smell is no good, but if nothing else, it’ll remind me which beers I want to drink again.
  • If I see one in a pub and circumstances permit, I’ll go for it. Must remember to take a photo otherwise it doesn’t count.
  • Learn something about the different types of beer along the way.

Here’s what I won’t do:

  • Start ‘300 More Beers To Try Before You Die!’
  • Start ‘1001 Beers You Must Taste Before You Die’
  • Splurge the contents of 300 Beers to Try Before You Die! I’ve seen it going for £5 at a The Book People sale, otherwise you can get it from Amazon

So, that’s it. I might as well get started.

How about you?

Have you tried to drink your way through the book?

Did you have any difficulty obtaining any of the beers? How did you overcome that?

Let me know in the comments below.

 

My First Non-Organic Namesake

July 29, 2013

I don’t normally take much interest in boats, particularly fishing boats, but this one caught my attention:

http://www.newsguardian.co.uk/news/local/fisherman-angered-as-boat-suffers-damage-from-a-suspected-collision-1-5869598

I’d love to know the history of how this boat was named ‘James Denyer’.

Edit: Well, this answers my question :)

http://www.culturescape.co.uk/mobile/mobile%20pages/clients/ntc/arts%20service/shimmer%20pages/shimmer%202012/Robert%20Oliver.html

Redirects From My Old Site

May 22, 2013

It’s a little later than planned, but I’ve finally redirected the majority of my old Classic ASP pages to the equivalent URL on this WordPress site. There are a couple of minor pages which I’m happy to let go of which will now be redirected to the default 404 error page for this site.

Theme Tinkering

May 10, 2013

OK, a quick update. I’ve uploaded all of the original content, the next step is to get the layout correct. Things may look a little wrong…

Hello world (again)!

April 22, 2013

Woo! After years of procrastination I’ve finally switched to WordPress because it’s all so much easier than coding your own site :)

Please bear with me whilst I rebuild.

Thanks

James