Search This Blog

27 May, 2019

Blog Information

This is a personal blog. The opinions expressed here represent my own and not those of my employer.

All content provided on this blog is for informational purpose only. I am solely responsible for all content published here and any views or opinions expressed here are strictly my own. Content published here is not read, reviewed, or approved in advance by any company and does not necessarily represent or reflect the views or opinions of my employer or any of its divisions, subsidiaries, or business partners.

The owner of this blog makes no representations as to the accuracy or completeness of any information on this site or found by following any link on this site. The owner will not be liable for any errors or omissions in this information nor for the availability of this information. The owner will not be liable for any losses, injuries, or damages from the display or use of this information. You can use this information on your own risk.While every caution has been taken to provide most accurate information , please use your discretion before taking any decisions based on the information in this blog.
Articles cannot be used, reprinted, or published without my written consent. Everyone can use the scripts/commands provided here without any written permission but owner will not be liable for any damage, loss to the systems.

25 May, 2019

Empty lines and too many spaces, fixing formatting with PowerShell

So you get a file from the boss and its formatting is little to be desired and it the case it has space in places where spaces just should not be.

It is time for a friend in the Windows, Linux, and macOS, PowerShell. 🦸‍♂️
That is right this little thing runs and works in all OSs. So you can have your cake and eat it too.

On with the show. 

So you get a file listONames.csv that you are asked to do something in Active Directory with. That part is not important that the moment because when you look at the data it looks like it will cause issues with a for each loop need in your code.

FirstName,LastName,SAMAccount

   Edward,Thomas,215ethomas  
Michael,Thomas,556mthomas  
      Mike,Jakison,369mjakison

Joe,Yaknow,786jyaknow

Time for our friend to fix the issue.

The first thing we need to is getting the content from the csv file into our little program to help file the issue.

PowerShell is really fun in its uses of a none and verb for its cmdLets (small functions or methods to get things done) to help you know and find what to uses.

In this case, we need to get some so we know it will start with "Get." The next thing we know is what we are getting, content. 

Those good folks at Microsoft have given us a function the is just that Get-Content for our cmdLet.

Type:
  • PS /home/Edward/Desktop> Get-Content ./listONames.csv
As to PowerShell command prompt, we type in get-connect and it pipes it back out to our screen.

Very cool because now we can do something with that. 

PowerShell has the idea or a piping operation but also treats the thing you uses as objects. Those object each come with things we can use to help us do the cool thing and get work done right.

So we have the content coming and we want to do something where if there are spaces we can trim those spaces right off. 

Type:
  • (Get-Content ./listONames.csv).Trim()
The "()" brackets say to do this operation and the "." at the end of the bracket says that this object within the bracket does an available function on that type of object. The function we are using on the list object is to trim the spaces before and after any characters in a listed line.

Goodbye, odd spaces and on your ways to awesome code.

Type:
  • (Get-Content ./listONames.txt).Trim() | Where-Object{$_.length -gt 0}
Now we need to get those loop killing empty line. Okay, it might not kill a for each loop but it can make odd results with what you are doing.

We need to go those each line and dump those blank lines so we will use the Where-Object cmdlet to do the heavy lifting. The "$_" is the way in PowerShell to say make a veritable on the fly from the piped in object and use toss it out the pipe for the use of the next thing in our list. 

We are trying to find something about our piped I object and if that object matches it we want that thing to move on through to the other side.

To help us with we will need to use a function "method" our object has built into its object class, length. The length is the count on characters in the line we are looking at. We want to compare that number with a minimum number or great then that number and "-gt" will help us do just that. In our case here we are saying any number greater than zero will do the trick.

Type:
  • (Get-Content ./listONames.txt).Trim() | Where-Object{$_.length -gt 0} | Set-Content ./fixedListONames.txt
 So now we need to save a new fixed list of names and set it's content to what we are piping out of that little bit of magic we are making. We could do something more with it but I think saving it off for later uses is a good please to stop.

Type:
  • Get-Content .\fixedListONames.csv

FirstName,LastName,SAMAccount
Edward,Thomas,215ethomas
Michael,Thomas,556mthomas
Mike,Jakison,369mjakison
Joe,Yaknow,786jyaknow

Got a question or help with a problem that you would like an answer to? Drop me a note and it could be in my next post.

email me at edward@programmingeveryday.com

An introduction:

 

Working with PowerShell and C# is a ton of fun. I have used PowerShell and C# in my job roles every day for the past 10 or so years. 

So what have I learned in those years? 

Well, a lot. My hope is that I can impart some of that knowledge I have picked up here and there to and help you on your paths in the world of IT and programming for a living. 

I am not much of a writer, as my fifth grade can attest to. I will, however, try my best to describe the ins and outs of my methods and practices in a clear and concise way as possible.

So come with me on this journey into the world of fun and problem solving with code in an everyday way.