I have had to generate a lot of new files lately, and because I am allergic to repetitive work, I looked for a way to dynamically generate and process files. And for my case, PowerShell scripting comes pretty close to what I needed. Just like a few of my other posts (the best of StackOverflow and Things I always have to Google, I decided it would be a good idea to centralize these scripts and share them with you. Most of these scripts were made for one specific case so they’re not generic – but upon request I’ll try to make these as generic as possible. This is going to be a post that I’ll hope to update on regular occasions, so make sure to pay a visit every now and then if you’re working with PowerShell too.
Scripts in this post
- Get a list of files
- Find & Replace
- Create file with template
- Call PowerShell scripts using bat files
Get a list of files in a directory
This next script will display a list of files with a .js extension in the directory in which the script is located.
Looking at the Get-ChildItem documentation, you notice there heaps of parameters that you can use:
In my code snippet, I used two parameters:
- Filter < string >: *.js indicates I want all files that end with .js. If you omit this parameter, the script will return all files.
- Recurse or -rec: by setting this flag the script will look recursively through all subfolders for files.
This script is fairly straightforward so the other parameters kind of speak for themselves. The documentation also is pretty easy to understand!
Find and Replace file contents
Create file with template
I had to write the following script to create a file using a template with placeholders. You’ll see this script will be useful when you get to the next script. There’s one parameter in this case called “cultureName”. A new file with the parameter’s value as the name will be created subsequently. You’ll also see the path is hardcoded, but it’s easy to make this part more generic. There’s something else going here too: the initial content of the file is also specified here. I have declared a variable $localeTemplate that contains the information. Note that this variable is a multiline string: you can achieve this by using the @”YOUR MULTILINE CONTENT HERE “@ syntax. Ultimately we fire up the PowerShell Set-Content method and we pass two parameters to this command: the path and the value. This cmdlet too has a lot of parameters, and again the documentation on technet is pretty easy to follow.
Bulk file creation with file template
The next script will only work if you have the previous script set up in the same directory. What this script will do is read the contents of a text file line by line and execute the previous script with a parameter. For example, the contents of the input file could look like this: nl-NL nl-BE en-US en-GB it-IT If you run this script, the loop will execute 5 times, and the ps1 script will be executed 5 times. Note how the invoke-expression cmdlet is called! The quotes are there for a reason! This is a dynamic expression where we call the ps1 script with 1 parameter (although there could be more of course).
Call PowerShell scripts using bat files
When I was still working with SharePoint a lot, we provided the system engineers an installation folder that contained everything they needed to deploy applications to test, user acceptance and production in an easy manner. The installation folder contained WSPs, dll, documentation files, but is also contained some PowerShell scripts and usually 1 batch file. You could say the batch file is the InstallShield wizard for the system engineers, because they can fiddle around with the parameters there (and nothing else!). You’ll need to create a new file and give it a .bat extension. Open the file in any editor of your choice and paste this on the first lines:
To provide some context, here’s how the test powershell script looks like:
Very simple script that outputs the parameters. If you then run the bat file, you’ll see that the powershell script is fired twice and the text is outputted twice too. Note that you can use named parameters here so you don’t have to worry about the order of the arguments. This approach has proven to be very successful among both developers and system administrators. Ideally, the administrators create the powershell deployment scripts themselves but even if they don’t, if the scripts are there, the administrators only have to worry about the parameters.