Bookmark and Share

In this post:

 

How Do I Create Shortcuts in Windows PowerShell?

Hey, Scripting Guy! Question

Hey, Scripting Guy! How do I create a Windows shortcut in Windows PowerShell? For example, I am installing Windows PowerShell these days on every Windows computer I touch. I would like to have a one-liner that creates the following shortcut:

C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe

I want to add that shortcut to the Quick Launch bar that is seen here:

C:\Documents and Settings\USERNAME\Application Data\Microsoft\Internet
Explorer\Quick Launch

-- JW

 

Hey, Scripting Guy! AnswerHello JW,

Microsoft Scripting Guy Ed Wilson here. You create shortcuts in Windows PowerShell in the same way you used to do it in VBScript. You use the Wscript.Shell COM object. The CreateShortCut.ps1 script illustrates this technique.

CreateShortCut.ps1

$strProg = "notepad"
$strFolder = "c:\fso\"
$wshShell = New-Object -ComObject WScript.Shell
$shortcut = $wshSHell.createshortcut($strFolder+$strProg+".lnk")
$shortcut.targetpath = $strProg+".exe"
$shortcut.iconlocation = "$strProg.exe,0"
$shortcut.description = "Mred waz here"
$shortcut.save()

If you want to do it in a single line, you can use the following syntax:

$shortcut = (New-Object -ComObject WScript.Shell).createshortcut(`

"c:\fso\notepad.lnk");$shortcut.targetpath = "notepad.exe"; $shortCut.Save()

If you create a New-ShortCut function that you place in your Windows PowerShell profile, it would be much easier.



Tell Me More About Try/Catch/Finally Blocks

Hey, Scripting Guy! Question

Hey, Scripting Guy! I noticed you do not use Try/Catch blocks in your blog scripts. Is that to make the samples readable/explainable or for some other reason?

-- JH

 

Hey, Scripting Guy! AnswerHello JK,

Hello JH, This is exactly the reason. In the scripts I write for the Hey, Scripting Guy! Blog, I generally have one or two specific goals for the scripts. If I load up the scripts with a lot of error catching, command-line help, and the like, the scripts get very long and have tendency to become rather complicated. I have written in the past about using Try/Catch/Finally blocks, and I will do so again when it makes sense to do so. Please keep in mind that the scripts I write are for purposes of learning and illustration. They are not complete, production-level scripts. To get a sense of what would be involved in creating robust, production-level scripts, refer to my Windows PowerShell 2.0 Best Practices book.


 

Troubleshooting a VBScript Script

Hey, Scripting Guy! Question

Hey, Scripting Guy! You have helped me many times in the past, and it is much appreciated. However, I am stumped on what should be a simple little script. When I run the script, it generates an error on the lines of code seen here:

wscript.echo

oFile.FileName  

The error, which I have received many times before, is seen in the following image. Usually, the error is because of a typo, but I do not think that is the case today.

Image of the error generated

I need to know how to find and display the property oFile.DateCreated. In addition, are the other properties oFile.FileName and Ofile.Extension not supported? I have never seen this before, and I need to use them. Could this be a broken Windows Script Host? I am running Windows 7, by the way.

Thanks again for your help. I consider myself good at scripting, but this one is a head scratcher.

Purge_DBINFO_Files.vbs (this is my script that doesn’t work)

'*************************************************************************************************
' File: Purge_DBINFO_Files.vbs
'
'' This script will delete files over a given age (in days) from a given directory (Path 1)
'
' There is no prompting or dialog boxes presented during this process...
' the files will just be gone
'*************************************************************************************************

' define the directory to be purged
Path1 = "C:\Tomcat\"

Dim fso
Dim oFolder
Dim oFile

   Set fso = createobject("Scripting.FileSystemObject")
 
   Set oFolder = fso.GetFolder(path1)
 
' set the number of days here... change the '1' to whatever
  For Each oFile In oFolder.files
       If DateDiff("d", oFile.DateCreated,Now) > 1 Then
wscript.echo oFile.DateCreated
wscript.echo oFile.FileName

        oFile.Delete True
    End If
  Next

' clean up
Set oFolder = Nothing
Set fso = Nothing

 

-- SS

 

Hey, Scripting Guy! AnswerHello SS,

The files collection object is returned from the Files property. The file object is an individual file from the collection. From the links on MSDN, you can see that there is no FileName property on a File object. So the error message is correctthere is no FileName property on the object. The property you are looking for is Name, not FileName.

In your code, a section of which is pasted below, you will need to make the change where indicated in red.

  For Each oFile In oFolder.files
       If DateDiff("d", oFile.DateCreated,Now) > 1 Then
wscript.echo oFile.DateCreated
wscript.echo oFile.Name

        oFile.Delete True
    End If
  Next


 

How Can I Determine Drive Speed? 

Hey, Scripting Guy! Question

Hey, Scripting Guy! I can almost always find what I am looking for (or just write it myself) but this one has me stumped. I need a script that will tell me the drive speed (for example, 5400 RPM, 7200 RPM). As far as I can tell, there is nothing built into the operating system that will give me this. I have found that we can measure the read and write speed, but I am not sure I am heading down the right road here so I figured (after all these years) that I would finally write and ask the experts!

Please help me. I really need this script for a project and need to query 6,000 workstations in 40 offices around the world.

-- PJ

 

Hey, Scripting Guy! AnswerHello PJ,

I cannot find this information directly. It might be in the registry, but I am unsure. There is no WMI class that directly reveals this information. You could get an idea of drive speed by using one of the WMI performance counter classes. An example of this is seen in the following Windows PowerShell code (gwmi is the alias for the Get-WMIObject cmdlet).

gwmi Win32_PerfFormattedData_PerfDisk_PhysicalDisk

Keep in mind that much of the counter information needs to be obtained via an account with administrator rights on Windows Vista and later. In addition, some of the disk counters must be specifically enabled and disabled by using the Diskperf utility, depending on the version of the operating system you are using. The commands are seen here:

diskperf -y

diskperf -n


I wrote about using WMI performance counter classes on November 24, 2008.

In addition to using the WMI performance counter classes, you can obtain information from WinSAT. There is a command-line utility that must be run as an administrator, Winsat.exe. Tell it you want the disk test, and specify -drive c: or whatever. For help, use winsat -? You can run this inside a Windows PowerShell console, as shown here:

PS C:\Windows\system32> winsat disk -drive c:
Windows System Assessment Tool
> Running: Feature Enumeration ''
> Run Time 00:00:00.00
> Running: Storage Assessment '-drive c: -seq -read'
> Run Time 00:00:10.08
> Running: Storage Assessment '-drive c: -ran -read'
> Run Time 00:00:11.40
> Running: Storage Assessment '-drive c: -scen 2009'
> Run Time 00:01:19.12
> Running: Storage Assessment '-drive c: -seq -write'
> Run Time 00:00:16.24
> Running: Storage Assessment '-drive c: -flush -seq'
> Run Time 00:00:07.75
> Running: Storage Assessment '-drive c: -flush -ran'
> Run Time 00:00:14.49
> Running: Storage Assessment '-drive c: -hybrid -ran -read -ransize 4096'
NV Cache not present.
> Run Time 00:00:00.11
> Running: Storage Assessment '-drive c: -hybrid -ran -read -ransize 16384'
NV Cache not present.
> Run Time 00:00:00.11
> Disk  Sequential 64.0 Read                   47.46 MB/s          5.2
> Disk  Random 16.0 Read                       1.45 MB/s          3.9
> Responsiveness: Average IO Rate              1.76 ms/IO          7.2
> Responsiveness: Grouped IOs                  12.89 units          6.5
> Responsiveness: Long IOs                     8.20 units          7.5
> Responsiveness: Overall                      105.79 units          6.8
> Responsiveness: PenaltyFactor                0.0
> Disk  Sequential 64.0 Write                  46.23 MB/s          5.2
> Average Read Time with Sequential Writes     9.371 ms          4.7
> Latency: 95th Percentile                     58.193 ms          1.9
> Latency: Maximum                             294.400 ms          7.0
> Average Read Time with Random Writes         20.053 ms          2.4
> Total Run Time 00:02:20.40
PS C:\Windows\system32>

If you do not need to create a new assessment, use WMI to directly retrieve the WinSAT disk score that is stored on the machine. This is seen here:

PS C:\Windows\system32> (gwmi win32_winsat).Diskscore
5.3


 

Can I Use VBScript to List All Computers Encrypted with BitLocker?

Hey, Scripting Guy! Question

Hey, Scripting Guy! Is it possible to create a VBScript script to list all computers that are encrypted with BitLocker.

-- JD

Hey, Scripting Guy! AnswerHello JD,

Use the Win32_EncryptableVolume WMI class and call the GetEncryptionMethod method. This class and its attendant methods are documented on MSDN.

 

Well, this concludes another edition of Quick-Hits Friday. Join us tomorrow for Weekend Scripter as we delve into the mysteries of…well, we will let that remain a mystery for now.

If you want to know exactly what we will be looking at tomorrow, follow us on Twitter or Facebook. If you have any questions, send e-mail to us at scripter@microsoft.com or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

 

Ed Wilson and Craig Liebendorfer, Scripting Guys