Tag Archives: Scripting

Friday Fun: Creating Sample Files

documentNot too long ago I came across a PowerShell snippet, probably via Twitter, that showed how to use a few classes from the System.IO namespace to create dummy files. Sadly I forgot the record where I saw this first mentioned. What I liked about the code I saw was the ability to create a dummy file of any size. You’re probably wondering why you would ever want to do this. Way, way back in the day when storage was expensive I used to create a few 100MB of dummy files using an old command line utility which I think was called creatfil.exe. The reasoning was that if the server started running out of disk space unexpectedly, I could delete the dummy files and buy myself a little time and breathing room. Fortunately, I think we’ve advanced where that technique is no longer necessary.

But, where you might want some dummy or sample files, is to simulate a user folder with a variety of files. You could use this test directory to hone your PowerShell scripts. Any you may have your own reasons for needing a bunch of sample files. So I took the original code and ran with it, as I usually do. I came up with a function called New-SampleFile.

The function will create a randomly sized file in the specified folder. The default is the current location. As you look through the code you’ll see I’ve added a test to verify the path and to make sure it is a FileSystem path and not something like the registry. Also by default the function will use a randomly generated file name which will be something like rcstwqyg.34e. Personally, I’m going to specify a filename. The function will create the file of a random size between 5 bytes and the value of the MaximumSize parameter, which has a default of 100KB.

The function will not overwrite existing files unless you use –Force. Nor will you see any output unless you use –Passthru. Even those are common parameters, I had to write the PowerShell expressions to implement them. The same is true of –WhatIf. You’ll see in the cmdletbinding attribute that the function supports ShouldProcess. However, the .NET classes I’m calling have no concept of WhatIf. I had to handle that myself

Here’s the function in action:

This also makes it easy to create multiple files.

Or how about this? I want to create a variety of files with different names.

Now I have some test files I can work with

Normally I prefer to use cmdlets wherever possible. But since there is no cmdlet from Microsoft to create dummy files, I have no problem resorting to the .NET Framework. However, I think it is still worth the time to wrap the .NET commands in a user-friendly and easy to use advanced function as I’ve done today.

If you find a way to use this function, I hope you’ll let me know.

Friday Fun: Search Me

magnifying-glass-text-label-search I’ve been working on a few revisions and additions to my ISE Scripting Geek module. Even though what I’m about to show you will eventually be available in a future release of that module, I thought I’d share it with you today. One of the things I wanted to be able to do was search Google (or Bing) from within the PowerShell ISE.

If you research search techniques or APIs for sites like Google and Bing, you’ll quickly get swallowed up in the miasma of JSON, SOAP, XML and mind-numbing examples. Although you might enjoy this sort of thing, I was hoping to find something much, much simpler. After all, I’m not going to be creating complicated search queries. I’m going to want to search for a simple term or phrase like “powershell remoting” or “Invoke-Webrequest”.

For Google, it is this simple to create a URL:

To use Bing, it seems it helps to include your culture which you can retrieve with Get-Culture.

In either case I now have an appropriate URL ready to go into my default browser. I can’t guarantee this will work for everyone, but it is as simple as this command:

I’m actually using the alias for Start-Process but in this case I don’t mind. Windows will switch to my browser and open the URL in a new tab. To make all of this a bit more flexible I put together a simple function.

The function needs a string of text to search for and a search engine. I’ve set a default to Google. You’ll also see that I’ve included parameter validation so that you can only use a value in a pre-defined set. To make the function a bit more interesting I also included Yahoo. I’ve used a Switch statement to determine how to construct the url. You could use this function from either the console or the PowerShell ISE. I suppose in terms of PowerShell this is a “low-tech” approach but it works and meets my needs.

To plug this into the ISE, you need to dot source the function in the ISE. Then if you want, you can add it as a menu item.

This will add a menu shortcut to the Add-Ons menu called “Search Internet”. I’ve also included an optional keyboard shortcut ALT+S, but you can define anything that isn’t already being used. When the menu item is invoked, it will run my Get-SearchResult function and use the currently selected text for the -Text parameter. The net result is a Google search for whatever I have selected in a script file. If you prefer Bing, I’d simply modify the function and set it as the default.

This isn’t rocket science but hopefully makes your PowerShell experience a little more efficient. You are welcome to try this now, or wait for the next version of the ISE Scripting Geek.

Have a great weekend.

Friday Fun: Updated ISE Scripting Geek Module

geek A few years ago I published a module with a number of functions and enhancements for the PowerShell ISE. This ISEScriptingGeek module has remained popular over the last few years. But I wrote it for PowerShell v2. I have also come up with a number of new additions to the ISE that I use to make scripting easier. So I have gone ahead and updated the module.

The new module includes most of the original features, many of which are added to a custom add-ons menu.

scriptinggeek-addon-menu

To use the module, import it in the ISE or in your ISE profile script. Once imported you can view all of the commands.

Most of the commands will have help. I’m not going to go into detail about every command and menu option. I incorporated my scripting help module as well to provide documentation on some of the validation attributes. I think you should load up some test files and try things out.

The final thing I have done is to include some ISE theme files. To import them, go to Tools – Options and click on Manage Themes. Click on Import and navigate to the Themes folder in the module directory (…\documents\WindowsPowerShell\Modules\ISEScriptingGeek\Themes). You should see 3 files. Import one or more, select it and click OK.

grayscaletheme

monoyellow-theme

vim-theme
This version requires PowerShell 4.0 or later. DownloadISEScriptingGeek-v3.1 and extract to your modules directory.

If you have questions or run into a problem, feel free to post a comment. I especially would love to hear about your favorite features from the module.

Enjoy and happy scripting.