All posts filed under: Quick Tips

Using DropBox to share PowerShell profile scripts across computers

I started using DropBox for several months ago and I love the way it syncs information across several computer I own. I put all the scripts under development in a DropBox folder and work on them from any PC I have access to. And, the changes are always there on all PCs. There is versioning too. This is simply awesome. Apart from the usual stuff like sharing documents, scripts, and photos, I also use DropBox to share my PowerShell profile scripts across all these computers I work on. All this is done automatically. I just need to set it up once and all my computers receive the changes to my profile script almost instantly. This is how I have been doing it: First, I have DropBox setup on all my computers. Second, I created a folder within DropBox folder and called it PowerShellProfiles. After this, I copied all my PowerShell profile scripts to the PowerShellProfiles folder on DropBox. BTW, I use two different profiles. One for PowerShell console/PowerGUI and another just for ISE. This is …

Quick PowerShell Tip: Get Process commandline information

This morning I answered a quetion on TechNet Windows PowerShell forum regarding retrieving a process commandline arguments. You cannot do this using Get-Process cmdlet. I thought I should share it here as well. Let us look at an example. I started a PowerShell process by passing a .PS1 script name to it. For the sake of demo, this script just sleeps for a long time. I used Start-> Run option to run the following commandline   PowerShell.exe C:\scripts\Testravi.ps1    Get-Process has no commandline property to see the arguments I just sent to PowerShell.exe  However, Win32_Process WMI class provides the commandline arguments information. This calss has a property called CommandLine. So, we can use Get-WMIObject cmdlet to see the commandline arguments passed to PowerShell.exe. To do this,  

This will filter the process information for the PowerShell process and output the commandline property value.     

Quick PowerShell Tip: Check if a given path is absolute or relative

You may, sometimes, want to check if a user provided file/folder path is relative or absolute and run the appropriate action based on the result. The System.IO.Path name space provides a simple method to verify that. That method is IsPathRooted(). This method will return True if the path is absolute and False if it is relative. The usage of this is quite simple.

This will result in $false as the path we provided is relative path. This is useful especially when we are validating parameters to a function. You can just use this as a part of ValidateScript. For example,

Test-Path1 will validate the parameter and continues only if the value of the parameter is absolute path.

Quick PowerShell Tip: Generating random filenames

While answering a question on Windows PowerShell forum, I came across a few interesting methods in the System.IO.Path namespace. One such method is GetRandomFileName(). This method can be used to generate a random, and cryptographically strong string that you can use as a folder or a file name. The usage is quite simple.

Also, there is another method GetTempFileName(). This method also generates a random (kinda.!) file name but always with a .TMP extension. Not just that, it also a creates a zero byte file on the disk and returns full path to the file.

There are many uses to these two methods. More on that later.!