February 26, 2016

The LS Thumping

Hello People, Grumpy Admin – The boss asked me a question and it was one of them simple things that make you want to go ninja and take an axe to the server room and perform some quality BOFH murder on people!  You won’t like Grump Admin when he is tried and has a lack of coffee in the morning!

This state of anger wasn’t caused because it was complex question, not that it was even a silly question but because rather than use the fantastic self-help that PowerShell provides he asked me, before I got my morning coffee, checked my emails… Right in there with a technical question that hits you in balls!

The question.

I need to produce a directory listing but to exclude all the docx files – is it possible, or do I need to use excel?

Seriously – if you spent any time in PowerShell or around me or my blog posts, you will know the most used is get-help

Also we know that we don’t use DIR anymore and that LS is actually an alias for get-childitem cmdlet as the below screenshot shows you!

1

so let’s do a quick

get-help get-childitem

 

2

So as you can see his bloody answer was there in the dam syntax, why did you feel the need to pester me!

-exclude <string>

 

So we can do a very simple command to achieve the required solution as promptly explained to him!

get-childitem -exclude *.docx

or

GCI -exclude *.docx

or

ls -exclude *.docx

 

Why I had to be bothered, and disturbed over this I don’t know…  Don’t you just hate that!

let’s just prove that this works, as we know grumpy admin likes to confirm that shit works – but trust me confidence is very high but I feel the need to prove it to you!

a normal LS/GCI output

3

and the output from the

ls -exclude *.docx

4

Simple – doesn’t take a rocket scientist to do that or to work it out does it or to use the dam help function or Google for that matter…. Why poke Grumpy Admin before he has had his morning coffee! Was this a test?

but as I like to make try and turn all these things into a fun learning experience, I also showed him so other neat tricks – just to drag it out… you know, to labor the point!

So I showed him you could do other interesting things using the pipeline 😛 For example, if you wanted to get files that were written to within the last day. Which could be a common scenario solved by doing the following

ls | ? LastWriteTime -gt (get-date).AddDays(-1)

5

Don’t forget if you need to go back in time, like in this instance you have to add negative numbers in the date AddDays function which is part of get-date!

So that scenario was easy as ever to solve, here some screenshots for proof…. right time for a along hard earned coffee and break for the next few hours – it’s Friday so perhaps a bacon bap from the canteen is the order of the day!

6

The moral of this story is – DON’T ASK BONE questions to Grumpy Admin first thing in the morning, that you can use self-help or find the answer by using a simple Google.

Remember people – if you can think it PowerShell should be able to do it!

Hazzy