Tag Archives: Scripting

Finding Git Repositories with PowerShell

As part of my ongoing improvement process this year I am starting to use Git much more. Yesterday I posted an article with my PowerShell script to create a new project folder which includes creating a Git repository. My challenge has been that I don’t always remember what I have set up with Git and what I have not. So I put together a little PowerShell function to identify folders with Git repositories.

The function, Find-GitRepository, takes a top-level path as a parameter. The function then searches all sub folders searching for hidden directories called .git. For each directory, the function creates a custom object that includes the path and details about the Git repository. To get those values the command jumps to each folder where I parse the results of git branch and git log.

find git repositories with PowerShell

There’s probably a bit more information I could pull with Git but for now this seems to be enough for me. You can find the function as a gist in my Github repository.

As with my previous script, I assume you have the git command line tools in your path.  If you use Git I hope you’ll try it out and let me know what you think.

Creating New PowerShell Projects

I’ve been writing scripts since the early days of DOS batch files. Like many of you I simply stick them all in a folder and move on to the next project. Most of my work is just for me or writing projects so this methodology worked just fine for me. But I need to live in your world and in 2016 that means new tools and processes. For many people developing PowerShell tools, modules, and DSC resources this means some sort of versioning system and testing. This is especially true if you are moving into the world of DevOps.

So I’ve been learning and using Git and Pester with my new projects. As I find myself revisiting older projects I’m also retrofitting them with a Git repository and a few Pester tests. A major benefit for me is that every time I do this, I learn a bit more about these tools. I’ve taken some of this knowledge and created a project provisioning script I call New-Project. I know some of you have something similar.

The script takes a top level folder name, I use a default value of C:\Scripts, and a project name. Typically this will be the name of a module. The script creates a folder with the project name and then runs through a set of initialization steps:

  • creates and commits an empty Git repository (master)
  • creates and commits Dev branch
  • checks out the Dev branch
  • creates a README.md file outline
  • creates the outline of a project .ps1 file
  • creates a Tests subfolder
  • creates the outline of Pester test file.

So I might use it like this:


You need to have the Git command line tools installed and the Pester module. I am still debating a few additional features such as integrating the project with my GitHub repositories and including a license file. Maybe even an option to generate a module manifest. Perhaps you’ll try it out and suggest a few other new features. The file is stored as a Gist on GitHub.

I hope you’ll give it a whirl and let me know what you think.

Converting Text to HTML Revised

A few years ago I published a PowerShell function to convert text files into HTML listings. I thought it would be handy to convert scripts to HTML documents with line numbering and some formatting. Turns out someone actually used it! He had some questions about the function which led me to revisit it and realize there were some improvements to be made.

Part of the challenge with this project is that the function takes an existing text file and creates a much larger text file. On one hand this is to be expected because we are adding HTML to it.  But I wanted to find ways to keep the size down.

After some HTML style tweaking, I found a better way to preserve the  formatting without dramatically increasing the file size. I also added an option so that you can specify an alternate CSS file. If you don’t use one, then the function will insert a default style into the head section. I also gave you the option to specify your own document title.

The revised script, which includes defining an alias, is now a gist on GitHub.

The function behaves much like ConvertTo-HTML in that it doesn’t create an actual file. You still need to pipe the output to Out-File.  And you should use Out-File instead of the console redirection character (>) as that created a much larger version of the file.

Here’s an example of a converted file using the default style sheet.


If you want to create your own style sheet, be sure to use the setting white-space:pre; as this should retain document formatting. At least the best it can.

I hope you’ll find this helpful. Enjoy.