New File Shortcut

I’ve been looking at the File Server Resource Manager (FSRM) feature in Windows Server 2008 R2 a lot lately. One very nice part of FSRM is the ability to schedule typical file management tasks. One of the examples from the Microsoft storage team is to create a custom task to move old files to another server and leave a shortcut in place. The files are moved to a more appropriate server and the users can still find their files with practically no fuss. However, this requires Windows Server 2008 R2. If you aren’t there yet, you can use my PowerShell function, New-FileShortCut in your archiving process.

This will require PowerShell 2.0. But you should be able to run it from your Windows 7 desktop and manage a Windows Server 2003 file server.




































































































































































































































The function includes comment-based help to make it easier to understand and use.

image

I’ve also taken the liberty of including support for –Verbose, –WhatIf and –Confirm. Not that creating a shortcut is that serious but I thought it might be worthwhile as a demonstration. These are defined with the cmdletBinding statement.

[cmdletBinding(SupportsShouldProcess=$True,ConfirmImpact=”Low”)]

The Write-Verbose lines only execute if you use –Verbose. The SupportsShouldProcess setting is used when –WhatIf is specified. This block of code is configured to handle –WhatIf.






Using –WhatIf turns line 1 into a False statement so the shortcut is never saved.  The method value I’m using, $Target, is what PowerShell displays in the WhatIf statement. I could have used any variable. In my case it made more sense to show the target file. If –Whatif isn’t specified then this line is True and the file is saved.

The ConfirmImpact setting tells PowerShell to prompt me if –Confirm is passed. If you always want to be confirmed, you can set this value to “High”.

The function itself is a wrapper for the Wscript.Shell COM object that you use in VBScript. This object has a CreateShortcut() method. All my function is doing is creating a simple shortcut. By default the function doesn’t write anything to the pipeline, but you can use –Passthru to see the actual file. I couldn’t think of a good situation where you might want to use this in a pipelined expression so I kept it simple. Feel free to edit the function if you want to reverse this behavior.

Alright. Now that we have this function how can we use it in our file management task? Conceptually you would use Get-Childitem to retrieve what ever set of files you want to manage. Move them to the new location then create a shortcut in the original directory that points to the moved file.
















In this example I’m getting all CSV files from \\File01\Files. Each file is moved to the new destination and to keep it easy to follow I’ define values for the link file (did I mention it must end in .lnk?) and the target file. I want to make sure the link file keeps the same basename and is in the same directory. But just like that all the CSV files are moved, leaving shortcut links in their place. When the user goes to the share and double clicks the CSV icon, they’ll actually open the file from the other server.

You can download the script file here.

2 thoughts on “New File Shortcut

Comments are closed.