Search This Blog

27 May, 2019

Blog Information and Disclosures

Blog Information and Disclosures

Welcome to my personal blog! Here, you'll find a treasure trove of my thoughts, insights, and experiences in the ever-evolving world of programming and technology. Please keep in mind that the opinions expressed here are solely my own and do not reflect those of my employer.

All content provided on this blog is for informational purposes only. I take full responsibility for everything published here, and the views shared are strictly my own. My posts haven’t been read, reviewed, or approved by any company, so they may not align with the perspectives of my employer or any of its divisions, subsidiaries, or business partners.

While I strive to provide accurate and helpful information, I cannot guarantee that all code will work flawlessly in every situation. The technical landscape is diverse and continually changing, meaning that certain scripts or commands may need to be adapted to fit your specific environment or use case. I encourage you to test any code thoroughly in a safe environment before deploying it in production.

Additionally, I occasionally share my thoughts on non-technical topics such as philosophy or current events. These reflections are mine alone and are shared freely with the intent of fostering kind and honest discussion. I welcome any dialogue and will do my best to clarify any areas of discussion where needed.

I make no representations regarding the completeness or reliability of the content on this site or any links within it. I won’t be liable for any errors or omissions or for the unavailability of information. Use the content at your own risk! I highly recommend applying your discretion and judgment before making any decisions based on what you find here.

Please remember that articles cannot be used, reprinted, or published without my written consent. However, feel free to use the scripts and commands I share without needing permission. Just keep in mind that I won't be liable for any damages or losses to your systems resulting from their use.

Thanks for visiting, and happy coding! May your journey through the world of technology be both enlightening and enjoyable!

25 May, 2019

Empty lines and too many spaces, fixing formatting with PowerShell

Empty lines and too many spaces, fixing formatting with PowerShell

You’ve received a file from your boss, but its formatting leaves much to be desired. There are extra spaces where they shouldn’t be, and it's time to bring in our trusty friend, PowerShell! Whether you’re on Windows, Linux, or macOS, this tool works seamlessly across all operating systems, allowing you to have your cake and eat it too. 🍰

The Scenario

Imagine you have a CSV file named listONames.csv that you need to use for some Active Directory tasks. However, the formatting issues might cause problems in your code, particularly within a foreach loop.

Here’s what the data looks like:

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

Time for a Fix!

First, we need to get the content from the CSV file into PowerShell to tackle these formatting issues.

PowerShell is intuitive with its cmdlets (small functions or methods) that follow a "Verb-Noun" structure, making it easy to understand what you’re trying to do. In this case, we want to retrieve content, so we start with "Get".

The cmdlet you need is Get-Content. Here’s how to use it:

Get-Content ./listONames.csv

When you run this command, PowerShell will display the contents of your CSV file on the screen.

Working with Objects

PowerShell treats everything as objects, which means each line of your CSV can be manipulated with built-in methods. To clean up those pesky spaces, we’ll trim them off.

Use the following command:

(Get-Content ./listONames.csv).Trim()

The parentheses () instruct PowerShell to execute the command within, and the dot . at the end indicates that we want to apply the Trim() method to the resulting object. This method removes any leading or trailing spaces from each line.

Removing Empty Lines

Next, we want to eliminate any empty lines that could disrupt your processing logic. While they might not "kill" a foreach loop, they can produce unexpected results.

You can filter out empty lines with the Where-Object cmdlet:

(Get-Content ./listONames.csv).Trim() | Where-Object { $_.Length -gt 0 }

In this command, $_ is a placeholder for the current object being processed in the pipeline. We’re using the Length property to check the number of characters in each line. The -gt operator checks if the length is greater than zero, effectively keeping only non-empty lines.

Saving the Cleaned Data

Finally, let's save the cleaned-up list into a new file. Use the following command to write the output to fixedListONames.csv:

(Get-Content ./listONames.csv).Trim() | Where-Object { $_.Length -gt 0 } | Set-Content ./fixedListONames.csv

Now, you can verify that the cleaned data has been saved correctly:

Get-Content ./fixedListONames.csv

You should see:

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

Conclusion

And there you have it! With a few simple PowerShell commands, you can clean up a CSV file and make it ready for further processing in Active Directory or any other application. Happy scripting! 🦸‍♂️

An introduction

An introduction

Welcome to Programming Every Day!

Ever since I first dove into the world of technology at the age of 15, I’ve been hooked! What started as a curious exploration of computers has blossomed into a rewarding career spanning multiple decades. Along the way, I've dedicated myself to mastering various programming languages, with PowerShell and C# becoming my trusty sidekicks.

Every day, I tackle a range of challenges in my IT roles, and trust me—there’s never a dull moment! From debugging code to automating tasks, the thrill of problem-solving keeps my passion alive.

I’m excited to share the insights, tips, and techniques I’ve gathered throughout my journey with you. My goal is to help you navigate the fascinating world of programming and IT with confidence and creativity.

Now, I may not be the world’s best writer—my fifth-grade teacher could surely back that up! But I promise to do my best to describe my methods and practices in a fun and approachable way.

So, come along on this adventure as we explore the joys of coding, celebrate our victories, and tackle the challenges of programming together. Let’s make the world of tech a little more enjoyable, one line of code at a time!