<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.technet.com/utility/FeedStylesheets/atom.xsl" media="screen"?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-US"><title type="html">Hey! Scripting Guy</title><subtitle type="html" /><id>http://blogs.technet.com/scriptingguy/atom.xml</id><link rel="alternate" type="text/html" href="http://blogs.technet.com/scriptingguy/default.aspx" /><link rel="self" type="application/atom+xml" href="http://blogs.technet.com/scriptingguy/atom.xml" /><generator uri="http://communityserver.org" version="2.1.61025.2">Community Server</generator><updated>2008-02-12T12:31:00Z</updated><entry><title>How Do I Check Which Version of Windows PowerShell I'm Using?</title><link rel="alternate" type="text/html" href="http://blogs.technet.com/scriptingguy/archive/2009/05/27/how-do-i-check-which-version-of-windows-powershell-i-m-using.aspx" /><id>http://blogs.technet.com/scriptingguy/archive/2009/05/27/how-do-i-check-which-version-of-windows-powershell-i-m-using.aspx</id><published>2009-05-28T08:56:00Z</published><updated>2009-05-28T08:56:00Z</updated><content type="html">&lt;IMG class=nearGraphic title="Hey, Scripting Guy! Question" height=34 alt="Hey, Scripting Guy! Question" src="http://img.microsoft.com/library/media/1033/technet/images/scriptcenter/qanda/q-sm.jpg" width=34 align=left border=0 mce_src="http://img.microsoft.com/library/media/1033/technet/images/scriptcenter/qanda/q-sm.jpg"&gt; 
&lt;P&gt;Hey, Scripting Guy! With the release of the Community Technology Preview (CTP) 3 for Windows PowerShell, some of our staff have gone ahead and updated their computers. I am now beginning to worry that I need to check the version of Windows PowerShell that is installed on a computer before I run a script. I have two questions, the first of which is obvious: How do I do this. The second question is this: What the heck is a CTP anyway?&lt;BR&gt;&lt;BR&gt;- WS&lt;/P&gt;&lt;IMG height=5 alt=Spacer src="http://img.microsoft.com/library/media/1033/technet/images/scriptcenter/spacer.gif" width=5 border=0 mce_src="http://img.microsoft.com/library/media/1033/technet/images/scriptcenter/spacer.gif"&gt;&lt;IMG class=nearGraphic title="Hey, Scripting Guy! Answer" height=34 alt="Hey, Scripting Guy! Answer" src="http://img.microsoft.com/library/media/1033/technet/images/scriptcenter/qanda/a-sm.jpg" width=34 align=left border=0 mce_src="http://img.microsoft.com/library/media/1033/technet/images/scriptcenter/qanda/a-sm.jpg"&gt; 
&lt;P&gt;Hi WS,&lt;/P&gt;
&lt;P&gt;Let’s answer the second question first. What is a CTP? In the old days, when you were writing your computer program (probably poking holes in key punch cards—I still have some lying around), you would first analyze the requirements for the application, do some flow-charting to determine the inputs and the outputs, and then come up with a mock-up. After the customer gave you the go-ahead, you would develop a working prototype. We typically would refer to these things as an &lt;I&gt;alpha&lt;/I&gt;. We could continue to refine the product, work on things, and as we were working on the code many times we would realize that things maybe were not working the way we had envisioned that they would work. After we had things working pretty well, we would again show it to the customer. This is a &lt;I&gt;beta&lt;/I&gt;. Now we need to fix bugs. So we work real hard fixing the bugs. When we have things working well, we call that a &lt;I&gt;release candidate&lt;/I&gt;. Sometimes we go through several release candidates before we actually release the product. &lt;/P&gt;
&lt;P&gt;The problem with that approach was the long lead time between the pre-beta product, and the short lead time between the beta product and the released product. In other words, when a product hits beta, its features are pretty much set. So getting input from a customer during the beta phase is good for finding bugs, but it is bad for finding out if what you are making is actually what the customer wants. To resolve this situation, several groups at Microsoft came up with the idea of the &lt;I&gt;community technology preview&lt;/I&gt;(CTP), which allows for earlier feedback from the community to the product group doing the coding. Windows PowerShell 1.0 went through a CTP, and it worked out great. Windows PowerShell 2.0 is once again going through a CTP, and many changes to the product have been directly attributed to feature requests coming from our customers. So you can think of a CTP as not being feature complete or bug free. You can think of a beta as pretty much being feature complete, but not bug free. A release candidate is feature complete, and mostly bug free. When working with a release candidate, what you see is pretty much what you will get when the software finally ships.&lt;/P&gt;
&lt;P&gt;If you want to identify the version of Windows PowerShell that is running on your computer, you can read the &lt;B&gt;RuntimeVersion&lt;/B&gt; value from the registry. This registry key is seen here:&lt;/P&gt;&lt;IMG height=375 alt="Image of the RuntimeVersion registry key" src="http://img.microsoft.com/library/media/1033/technet/images/scriptcenter/qanda/hsg/2009/january/hey0105/HSG_rw1_01.jpg" width=500 border=0 mce_src="http://img.microsoft.com/library/media/1033/technet/images/scriptcenter/qanda/hsg/2009/january/hey0105/HSG_rw1_01.jpg"&gt; 
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One way to read from the registry is to use the registry provider from within Windows PowerShell and read a registry value as you might read a property from a file or a folder. To do this you need to use the HKLM PS drive—HKLM: in this case—and follow it with the path to the registry key, which is \SOFTWARE\Microsoft\PowerShell\1\PowerShellEngine. You can then select the &lt;B&gt;item&lt;/B&gt; property you are interested in, which is &lt;B&gt;RunTimeVersion&lt;/B&gt; in this example. This is seen in this script:&lt;/P&gt;&lt;PRE class=codeSample&gt;$path = "HKLM:\SOFTWARE\Microsoft\PowerShell\1\PowerShellEngine"
$psv = get-itemproperty -path $path
$psv.RunTimeVersion
&lt;/PRE&gt;
&lt;P&gt;People who are familiar with VBScript may wish to create the &lt;B&gt;WshShell&lt;/B&gt; object and use the &lt;B&gt;RegRead&lt;/B&gt; method. To do this we use the &lt;B&gt;HKLM&lt;/B&gt; moniker as a shortcut to refer to the &lt;B&gt;HKEY_LOCAL_MACHINE&lt;/B&gt; registry key. The &lt;B&gt;HKLM&lt;/B&gt; moniker when used with the &lt;B&gt;WshShell&lt;/B&gt; object is case sensitive. &lt;/P&gt;
&lt;P&gt;We store the path to the Windows PowerShell configuration information in the &lt;B&gt;$path &lt;/B&gt;variable. Next we use the &lt;B&gt;New-Object&lt;/B&gt; cmdlet to create an instance of the &lt;B&gt;WshShell&lt;/B&gt; object. This COM object has the program ID of &lt;B&gt;Wscript.Shell&lt;/B&gt;. We store the returned object in the &lt;B&gt;$wshShell&lt;/B&gt; variable. After we have the WshShell object, we use the &lt;B&gt;RegRead&lt;/B&gt; method to read the registry key value, which we specify by placing the path and value name in an expanding string: &lt;B&gt;“$path\RunTimeVersion”&lt;/B&gt;. This script is shown here: &lt;/P&gt;&lt;PRE class=codeSample&gt;$path = "HKLM\SOFTWARE\Microsoft\PowerShell\1\PowerShellEngine"
$WshShell = New-Object -ComObject Wscript.Shell
$WshShell.RegRead("$path\RunTimeVersion")
&lt;/PRE&gt;
&lt;P&gt;There may be times when you wish to use WMI to read the registry. To use WMI to read the registry, we need to use the &lt;B&gt;stdRegProv&lt;/B&gt; WMI class. The &lt;B&gt;stdRegProv&lt;/B&gt; WMI class has always been in the &lt;B&gt;root\default&lt;/B&gt; WMI namespace. Beginning with Windows Vista, we also have an instance of the &lt;B&gt;stdRegProv&lt;/B&gt; WMI class in the &lt;B&gt;root\cimv2&lt;/B&gt; (which incidentally really is the default namespace). What this means is that you can use the &lt;B&gt;stdRegProv&lt;/B&gt; WMI class from either the &lt;B&gt;root\default&lt;/B&gt; WMI namespace or from the &lt;B&gt;root\cimv2&lt;/B&gt; WMI namespace. It does not matter as it is the same WMI class. As a best practice, I recommend that you use the instance of the class from the &lt;B&gt;root\default&lt;/B&gt; WMI namespace (as is done in the &lt;B&gt;GetPsVersionWmi.Ps1&lt;/B&gt; script below) to ensure compatibility with older versions of the Microsoft Windows operating system. WMI uses coded values to determine the registry tree (also known as a hive). These coded values are seen in the table just below. HKEY_DYN_DATA registry tree only exists on Windows 95 and Windows 98.&lt;/P&gt;
&lt;P&gt;&lt;B&gt;Table 1 WMI registry tree values&lt;/B&gt;&lt;/P&gt;
&lt;TABLE class=dataTable id=EHG cellSpacing=0 cellPadding=0 class="dataTable"&gt;
&lt;THEAD&gt;
&lt;TR class=stdHeader vAlign=top&gt;
&lt;TD class="" id=colEJG&gt;Name&lt;/TD&gt;
&lt;TD class="" id=colENG style="BORDER-RIGHT: #cccccc 1px solid"&gt;Value&lt;/TD&gt;&lt;/TR&gt;&lt;/THEAD&gt;
&lt;TBODY&gt;
&lt;TR class=record vAlign=top&gt;
&lt;TD class=""&gt;
&lt;P class=lastInCell&gt;HKEY_CLASSES_ROOT&lt;/P&gt;&lt;/TD&gt;
&lt;TD class="" style="BORDER-RIGHT: #cccccc 1px solid"&gt;
&lt;P class=lastInCell&gt;2147483648&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR class=evenRecord vAlign=top&gt;
&lt;TD class=""&gt;
&lt;P class=lastInCell&gt;HKEY_CURRENT_USER&lt;/P&gt;&lt;/TD&gt;
&lt;TD class="" style="BORDER-RIGHT: #cccccc 1px solid"&gt;
&lt;P class=lastInCell&gt;2147483649&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR class=record vAlign=top&gt;
&lt;TD class=""&gt;
&lt;P class=lastInCell&gt;HKEY_LOCAL_MACHINE&lt;/P&gt;&lt;/TD&gt;
&lt;TD class="" style="BORDER-RIGHT: #cccccc 1px solid"&gt;
&lt;P class=lastInCell&gt;2147483650&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR class=evenRecord vAlign=top&gt;
&lt;TD class=""&gt;
&lt;P class=lastInCell&gt;HKEY_USERS&lt;/P&gt;&lt;/TD&gt;
&lt;TD class="" style="BORDER-RIGHT: #cccccc 1px solid"&gt;
&lt;P class=lastInCell&gt;2147483651&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR class=record vAlign=top&gt;
&lt;TD class=""&gt;
&lt;P class=lastInCell&gt;HKEY_CURRENT_CONFIG&lt;/P&gt;&lt;/TD&gt;
&lt;TD class="" style="BORDER-RIGHT: #cccccc 1px solid"&gt;
&lt;P class=lastInCell&gt;2147483653&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR class=evenRecord vAlign=top&gt;
&lt;TD class=""&gt;
&lt;P class=lastInCell&gt;HKEY_DYN_DATA&lt;/P&gt;&lt;/TD&gt;
&lt;TD class="" style="BORDER-RIGHT: #cccccc 1px solid"&gt;
&lt;P class=lastInCell&gt;2147483654&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;
&lt;DIV class=dataTableBottomMargin&gt;&lt;/DIV&gt;
&lt;P&gt;We use the value &lt;B&gt;2147483650&lt;/B&gt; and assign it to the &lt;B&gt;$HKLM&lt;/B&gt; variable. This value points the WMI query to the HKEY_LOCAL_MACHINE registry tree. We then assign the string &lt;B&gt;sof&lt;/B&gt;&lt;B&gt;t&lt;/B&gt;&lt;B&gt;ware\Microsoft\PowerShell\1\PowerShellEngine&lt;/B&gt; to the variable &lt;B&gt;$key&lt;/B&gt;. When using WMI to read the registry, the key is not preceded with a backslash. &lt;/P&gt;
&lt;P&gt;We assign the registry property value we wish to read to the &lt;B&gt;$value&lt;/B&gt; variable. Now we use the &lt;B&gt;[wmiclass]&lt;/B&gt; type accelerator to obtain an instance of the &lt;B&gt;stdRegProv&lt;/B&gt; WMI class. We specify the &lt;B&gt;root\default&lt;/B&gt; WMI namespace to specify which version of the &lt;B&gt;stdRegProv &lt;/B&gt;WMI class we wish to use. We could also have preceded the namespace with the name of a computer and read the registry from a remote computer. After we have created an instance of the &lt;B&gt;stdregProv &lt;/B&gt;WMI class, we use the resulting &lt;B&gt;Sy&lt;/B&gt;&lt;B&gt;s&lt;/B&gt;&lt;B&gt;tem.Management.Managementclass&lt;/B&gt; to call the &lt;B&gt;GetStringvalue&lt;/B&gt; method. The &lt;B&gt;GetStringValue&lt;/B&gt; method takes three arguments: the registry key coded value, the registry subkey string, and the property name. Two values are returned by the method call. The first returned value is the &lt;B&gt;returnvalue&lt;/B&gt;, which will indicate the success or the failure of the method call; the second is the &lt;B&gt;svalue&lt;/B&gt;, which is the string value that was stored in the registry property. The complete &lt;B&gt;GetPsVersionWmi.Ps1&lt;/B&gt; script is seen here:&lt;/P&gt;&lt;PRE class=codeSample&gt;$hklm = 2147483650
$key = "SOFTWARE\Microsoft\PowerShell\1\PowerShellEngine"
$value = "RunTimeVersion"
$wmi = [wmiclass]"root\default:stdRegProv"
($wmi.GetStringValue($hklm,$key,$value)).svalue
&lt;/PRE&gt;
&lt;P&gt;We can also use the .NET Framework classes to obtain information from the registry. To do this, we use the &lt;B&gt;Microsoft.Win32.Registry&lt;/B&gt; .NET Framework class. We use the &lt;B&gt;GetValue&lt;/B&gt; static method, which takes three parameters. The first parameter is the registry root and key name. The second parameter is the registry value you wish to read, and the last parameter is the default value of the registry key value. In the &lt;B&gt;GetPsVersionNet.Ps1&lt;/B&gt; script, we assign the &lt;B&gt;HKEY_LOCAL_MACHINE&lt;/B&gt; string value to the &lt;B&gt;$hklm&lt;/B&gt; variable. Next we assign the string representing the remainder of the registry path to the &lt;B&gt;$key&lt;/B&gt; variable. The registry key property value we wish to retrieve is stored in the &lt;B&gt;$value&lt;/B&gt; variable. We then use the &lt;B&gt;[Microsoft.Win32.Registry]&lt;/B&gt; class and use the two colons to signify that we wish to use a static method and then use the &lt;B&gt;Get&lt;/B&gt;&lt;B&gt;V&lt;/B&gt;&lt;B&gt;alue&lt;/B&gt; method with the &lt;B&gt;$hklm&lt;/B&gt;, &lt;B&gt;$key&lt;/B&gt;, and &lt;B&gt;$value&lt;/B&gt; parameters passed to it. The &lt;B&gt;GetPsversionNet.ps1&lt;/B&gt; script is seen here:&lt;/P&gt;&lt;PRE class=codeSample&gt;$hklm = "HKEY_LOCAL_MACHINE"
$key = "SOFTWARE\Microsoft\PowerShell\1\PowerShellEngine"
$value = "RunTimeVersion"
[Microsoft.Win32.Registry]::GetValue("$hklm\$key",$value,$null)
&lt;/PRE&gt;
&lt;P&gt;Well, WS, as you have seen, to work with the registry there is a Windows PowerShell provider, a COM object, a WMI class, and a .NET Framework class. This plethora of methodologies is one of the big strengths of Windows PowerShell. It is also a significant source of confusion for people just learning Windows PowerShell. What is the best thing to use? If you are just making the transition from VBScript, use whatever is most comfortable to you. &lt;/P&gt;
&lt;P&gt;I personally like using &lt;B&gt;regread&lt;/B&gt; from the &lt;B&gt;wshshell&lt;/B&gt; object. It is easy, fast, and makes sense. When I need to connect to a remote computer, I use WMI. I almost never use the .NET version. At times, however, I like the simplicity of using the Windows PowerShell provider. It makes it very easy to use. Stay tuned. We will be working with the registry all this week, and maybe by the end of the week you will find what works for you. See you tomorrow. Peace.&lt;/P&gt;&lt;img src="http://blogs.technet.com/aggbug.aspx?PostID=3246583" width="1" height="1"&gt;</content><author><name>TharikaC</name><uri>http://blogs.technet.com/members/TharikaC.aspx</uri></author><category term="srripting guy" scheme="http://blogs.technet.com/scriptingguy/archive/tags/srripting+guy/default.aspx" /></entry><entry><title>How Can I Delete and Manage PDF Files?</title><link rel="alternate" type="text/html" href="http://blogs.technet.com/scriptingguy/archive/2009/05/26/how-can-i-delete-and-manage-pdf-files.aspx" /><id>http://blogs.technet.com/scriptingguy/archive/2009/05/26/how-can-i-delete-and-manage-pdf-files.aspx</id><published>2009-05-27T03:58:00Z</published><updated>2009-05-27T03:58:00Z</updated><content type="html">&lt;IMG class=nearGraphic title="Hey, Scripting Guy! Question" height=34 alt="Hey, Scripting Guy! Question" src="http://img.microsoft.com/library/media/1033/technet/images/scriptcenter/qanda/q-sm.jpg" width=34 align=left border=0 mce_src="http://img.microsoft.com/library/media/1033/technet/images/scriptcenter/qanda/q-sm.jpg"&gt; 
&lt;P&gt;Hey, Scripting Guy! We are implementing a document management system at work, and users are no longer allowed to store PDF files. I need to search a folder for the existence of PDF files. If the folder contains any PDF files, I want to delete them. If on the other hand there are no PDF files in the folder, I would like to create a file called &lt;B&gt;nopdf.txt&lt;/B&gt;. This is because we will use &lt;A href="http://www.microsoft.com/systemcenter/configurationmanager/en/us/default.aspx" target=_blank mce_href="http://www.microsoft.com/systemcenter/configurationmanager/en/us/default.aspx"&gt;Systems Center Configuration manager&lt;/A&gt; software inventory to inventory computers that have the &lt;B&gt;nopdf.txt&lt;/B&gt; file.&lt;BR&gt;&lt;BR&gt;- RU&lt;/P&gt;&lt;IMG height=5 alt=Spacer src="http://img.microsoft.com/library/media/1033/technet/images/scriptcenter/spacer.gif" width=5 border=0 mce_src="http://img.microsoft.com/library/media/1033/technet/images/scriptcenter/spacer.gif"&gt;&lt;IMG class=nearGraphic title="Hey, Scripting Guy! Answer" height=34 alt="Hey, Scripting Guy! Answer" src="http://img.microsoft.com/library/media/1033/technet/images/scriptcenter/qanda/a-sm.jpg" width=34 align=left border=0 mce_src="http://img.microsoft.com/library/media/1033/technet/images/scriptcenter/qanda/a-sm.jpg"&gt; 
&lt;P&gt;Hi RU,&lt;/P&gt;
&lt;P&gt;That is a rather interesting idea. Back in the days when I used to work with Systems Management Server (SMS), I used to do a similar trick to allow me easily track the progress of various operations.&lt;/P&gt;
&lt;P&gt;For information about working with files and folders in Windows PowerShell see &lt;A href="http://www.microsoft.com/technet/scriptcenter/resources/pstips/oct07/pstip1005.mspx" target=_blank mce_href="http://www.microsoft.com/technet/scriptcenter/resources/pstips/oct07/pstip1005.mspx"&gt;this article&lt;/A&gt;. You may also wish to see &lt;A href="http://www.microsoft.com/technet/scriptcenter/topics/msh/cmdlets/files.mspx" target=_blank mce_href="http://www.microsoft.com/technet/scriptcenter/topics/msh/cmdlets/files.mspx"&gt;this section&lt;/A&gt; of "What Can I Do with Windows PowerShell" that deals specifically with files and folders. The &lt;A href="http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx" target=_blank mce_href="http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx"&gt;Windows PowerShell scripting hub&lt;/A&gt; is a good place to get started with Windows PowerShell, and it includes download links and other items of use. The &lt;A href="http://www.microsoft.com/technet/scriptcenter/resources/begin/archive.mspx" target=_blank mce_href="http://www.microsoft.com/technet/scriptcenter/resources/begin/archive.mspx"&gt;Sesame Script archive&lt;/A&gt; has several VBScript articles dealing with files and folders. They are worth a look for VBScript examples. The community script repository has a good selection of &lt;A href="http://www.microsoft.com/technet/scriptcenter/csc/scripts/files/default.mspx" target=_blank mce_href="http://www.microsoft.com/technet/scriptcenter/csc/scripts/files/default.mspx"&gt;files and folders scripts&lt;/A&gt; in VBscript. You also may want to check the &lt;A href="http://www.microsoft.com/technet/scriptcenter/resources/qanda/files.mspx" target=_blank mce_href="http://www.microsoft.com/technet/scriptcenter/resources/qanda/files.mspx"&gt;Hey, Scripting Guy! archive&lt;/A&gt; for a number of VBScript examples of working with files and folders.&lt;/P&gt;
&lt;P&gt;Interestingly enough, with Windows PowerShell we have a short syntax that can be used from the command line. If we wanted to search a folder to see if there are any PDF files in it, and then print out a message if there were none, we could use this kind of syntax:&lt;/P&gt;&lt;PRE class=codeSample&gt;if(!(gci C:\fso *.pdf -R)) { "no pdf"}
&lt;/PRE&gt;
&lt;P&gt;The problem with this type of syntax is that it is hard to read. This is the difference between working from the command line and writing a script. The above command begins by using an &lt;B&gt;if&lt;/B&gt; statement. The &lt;B&gt;if&lt;/B&gt; statement has the condition that is evaluated in smooth parentheses and the code that will be executed in curly brackets. In the &lt;B&gt;if&lt;/B&gt; statement, we use the &lt;B&gt;Get-ChildItem&lt;/B&gt; cmdlet (or “gci”) to look for PDF files in the &lt;B&gt;fso&lt;/B&gt; directory. The &lt;B&gt;-r&lt;/B&gt; means to recurse. The exclamation mark, &lt;B&gt;!&lt;/B&gt;, is used as the &lt;B&gt;not&lt;/B&gt; operator. This is similar syntax to when your friend comes up to you and says, "You are looking good today. Not." In the same way, the &lt;B&gt;not&lt;/B&gt; operator means if we do not find any PDF files in the &lt;B&gt;fso&lt;/B&gt; directory, we will print out "no pdf". Let's see how we would use this in a script. The script we came up with is called &lt;B&gt;CheckForPdfAndCreateMarker.ps1&lt;/B&gt;, and it is seen here.&lt;/P&gt;&lt;PRE class=codeSample&gt;$path = "c:\fso"
$include = "*.pdf"
$name = "nopdf.txt"
if(!(Get-ChildItem -path $path -include $include -Recurse)) 
  { 
    "No pdf was found in $path. Creating $path\$name marker file."
    New-Item -path $path -name $name -itemtype file -force |
    out-null
  } #end if not Get-Childitem
ELSE
 {
  $response = Read-Host -prompt "PDF files were found. Do you wish to delete &amp;lt;y&amp;gt; /&amp;lt;n&amp;gt;?"
  if($response -eq "y")
    {
     "Pdf files will be deleted."
     Get-ChildItem -path $path -include $include -recurse |
      Remove-Item
    } #end if response
  ELSE
   { 
    "PDF files will not be deleted."
   } #end else reponse
 } #end else not Get-Childitem
&lt;/PRE&gt;
&lt;P&gt;In the script, the first thing we do is define three variables. The first variable holds the path to the folder we are going to search, and we name it &lt;B&gt;$path&lt;/B&gt;. The second variable holds the files we will be searching for. Because this value gets supplied to the &lt;B&gt;-include&lt;/B&gt; parameter, I called the variable &lt;B&gt;$include&lt;/B&gt;. Lastly, we have the name of the text file we wish to create. I call it &lt;B&gt;$name&lt;/B&gt;. This section of code is seen here:&lt;/P&gt;&lt;PRE class=codeSample&gt;$path = "c:\fso"
$include = "*.pdf"
$name = "nopdf.txt"
&lt;/PRE&gt;
&lt;P&gt;As a best practice, when working with scripts I tend to make the variable name the same as the parameter name. This makes the code easy to read and easier to understand.&lt;/P&gt;
&lt;P&gt;Next we need to search for the PDF files. To do this, we use the &lt;B&gt;Get-ChildItem&lt;/B&gt; cmdlet. This cmdlet is great at searching folders, and it returns either a &lt;B&gt;fileinfo&lt;/B&gt; object or a &lt;B&gt;directoryinfo&lt;/B&gt; object, depending on whether the item found is a file or a folder. Because we want to search the folder specified in the &lt;B&gt;-path&lt;/B&gt; parameter and look for the filetypes (PDF) included in the &lt;B&gt;$include&lt;/B&gt;, variable we need to use the &lt;B&gt;-recurse&lt;/B&gt; parameter. We are only interested in the fact that the files either exist or don’t exist. This is a Boolean condition. Just like a light switch is either on or off, something is true or false, exists or does not exist. These are all Boolean conditions. You could use an &lt;B&gt;if&lt;/B&gt; statement like this in VBScript, but most people never did. While it might look a little strange at first, it is a nice construction. Note that the condition we are negating needs its own set of parentheses. Just like in high school algebra, things inside parentheses get operated upon before things outside the parentheses. Here is the line of code:&lt;/P&gt;&lt;PRE class=codeSample&gt;if(!(Get-ChildItem -path $path -include $include -Recurse))
&lt;/PRE&gt;
&lt;P&gt;In the code block for the &lt;B&gt;if&lt;/B&gt; statement, we first print out a message that states no PDF file was found, and we are going to create the marker file. The cool thing here is the way the expanding strings work. An expanding string is a double quotation mark. The advantage of an expanding string is that it will expand the value of a variable when it is placed in the quotation marks. Single quotation marks are literal quotation marks. What is placed inside them is what is printed out. Here are some examples of this:&lt;/P&gt;&lt;PRE class=codeSample&gt;PS C:\&amp;gt; $a = "string"
PS C:\&amp;gt; "$a is a string"
string is a string
PS C:\&amp;gt; '$a is a string'
$a is a string
PS C:\&amp;gt;
&lt;/PRE&gt;
&lt;P&gt;In our code block, after we have printed out a message, we then create the empty file in our folder. To do this we use the &lt;B&gt;New-Item&lt;/B&gt; cmdlet. &lt;B&gt;New-Item&lt;/B&gt; can be used to create files, folders, and other things as well. This is why it is called &lt;B&gt;New-Item&lt;/B&gt; instead of &lt;B&gt;New-File&lt;/B&gt;. To create a file, we use the &lt;B&gt;-itemtype&lt;/B&gt; of &lt;B&gt;file&lt;/B&gt;. To create a folder we use the &lt;B&gt;-itemtype&lt;/B&gt; of &lt;B&gt;directory&lt;/B&gt;. It is possible the file already exists in the directory. To handle this eventuality, we use the &lt;B&gt;-force&lt;/B&gt; parameter, which will cause the command to overwrite the existing file. We do not want to clutter the screen with confirmation messages, so we pipe the returned information to the &lt;B&gt;out-null&lt;/B&gt; cmdlet. This code is seen here: &lt;/P&gt;&lt;PRE class=codeSample&gt;{ 
    "No pdf was found in $path. Creating $path\$name marker file."
    New-Item -path $path -name $name -itemtype file -force |
    out-null
  } #end if not Get-Childitem
&lt;/PRE&gt;
&lt;P&gt;When no PDF files are found, we see this message:&lt;/P&gt;&lt;IMG height=248 alt="The message shown when no PDF files are found" src="http://img.microsoft.com/library/media/1033/technet/images/scriptcenter/qanda/hsg/hey1229/hsg_ff1_01.jpg" width=500 border=0 mce_src="http://img.microsoft.com/library/media/1033/technet/images/scriptcenter/qanda/hsg/hey1229/hsg_ff1_01.jpg"&gt; 
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In the &lt;B&gt;else&lt;/B&gt; clause, we need to handle the situation in which there are PDF files in the folder. We could just go ahead and delete the things, and then comment out the &lt;B&gt;Read-Host&lt;/B&gt; line and the &lt;B&gt;if&lt;/B&gt; and &lt;B&gt;else &lt;/B&gt;clause. But this also gives me a chance to show you how to use &lt;B&gt;read-host&lt;/B&gt; to solicit input from the user. The &lt;B&gt;Read-Host&lt;/B&gt; cmdlet displays a prompt. If you want to capture the user response, use a variable to hold the results of the cmdlet. In this example, I use the variable &lt;B&gt;$response&lt;/B&gt; to hold the user response. This section of the script is seen here:&lt;/P&gt;&lt;PRE class=codeSample&gt;ELSE
 {
  $response = Read-Host -prompt "PDF files were found. Do you wish to delete &amp;lt;y&amp;gt; /&amp;lt;n&amp;gt;?"
&lt;/PRE&gt;
&lt;P&gt;Do not use &lt;B&gt;$input&lt;/B&gt; for the input to the &lt;B&gt;read-host&lt;/B&gt; cmdlet, as &lt;B&gt;$input&lt;/B&gt; is an automatically created variable that is used for other things in Windows PowerShell. It will cause strange things to happen if you use this variable for your own purposes.&lt;/P&gt;
&lt;P&gt;When we run the script and PDF files are found, we are greeted with this message:&lt;/P&gt;&lt;IMG height=248 alt="The message shown when PDF files are found" src="http://img.microsoft.com/library/media/1033/technet/images/scriptcenter/qanda/hsg/hey1229/hsg_ff1_02.jpg" width=500 border=0 mce_src="http://img.microsoft.com/library/media/1033/technet/images/scriptcenter/qanda/hsg/hey1229/hsg_ff1_02.jpg"&gt; 
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;We need to evaluate the response. To do this we use the &lt;B&gt;if&lt;/B&gt; statement. In Windows PowerShell, we do not use the equals sign (&lt;B&gt;=&lt;/B&gt;) for the equality operator. Instead, we use &lt;B&gt;-eq&lt;/B&gt;, &lt;B&gt;-ieq&lt;/B&gt;, and &lt;B&gt;-ceq&lt;/B&gt; for equality. The first one is case insensitive, the second is likewise case insensitive, and the last one is the case sensitive equality operator. If the value of &lt;B&gt;$response&lt;/B&gt; is equal to "y", we print out a message that states the PDF files will be deleted, and we once again use the &lt;B&gt;Get-ChildItem&lt;/B&gt; cmdlet to provide a collection of all the PDF files in the folder. The cool thing is we pipeline the resulting information to the &lt;B&gt;Remove-Item&lt;/B&gt; cmdlet, which deletes the files. This is seen here:&lt;/P&gt;&lt;PRE class=codeSample&gt;if($response -eq "y")
    {
     "Pdf files will be deleted."
     Get-ChildItem -path $path -include $include -recurse |
      Remove-Item
    } #end if response
&lt;/PRE&gt;
&lt;P&gt;When the user types "y", the screen changes as seen here:&lt;/P&gt;&lt;IMG height=248 alt="The screen changes when the user types 'y'" src="http://img.microsoft.com/library/media/1033/technet/images/scriptcenter/qanda/hsg/hey1229/hsg_ff1_03.jpg" width=500 border=0 mce_src="http://img.microsoft.com/library/media/1033/technet/images/scriptcenter/qanda/hsg/hey1229/hsg_ff1_03.jpg"&gt; 
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But if the value of &lt;B&gt;$response&lt;/B&gt; was anything other than "y", we print out a message that the files will not be deleted and we exit the script. This is seen here:&lt;/P&gt;&lt;PRE class=codeSample&gt;ELSE
   { 
    "PDF files will not be deleted."
   } #end else reponse
 } #end else not Get-Childitem
&lt;/PRE&gt;
&lt;P&gt;RU, hope U R satisfied with this script. Get it? Script you again soon.&lt;/P&gt;&lt;img src="http://blogs.technet.com/aggbug.aspx?PostID=3245985" width="1" height="1"&gt;</content><author><name>TharikaC</name><uri>http://blogs.technet.com/members/TharikaC.aspx</uri></author><category term="files and folders" scheme="http://blogs.technet.com/scriptingguy/archive/tags/files+and+folders/default.aspx" /></entry><entry><title>How Can I Password-Protect an Excel Spreadsheet?</title><link rel="alternate" type="text/html" href="http://blogs.technet.com/scriptingguy/archive/2008/12/03/how-can-i-password-protect-an-excel-spreadsheet.aspx" /><id>http://blogs.technet.com/scriptingguy/archive/2008/12/03/how-can-i-password-protect-an-excel-spreadsheet.aspx</id><published>2008-12-03T17:35:00Z</published><updated>2008-12-03T17:35:00Z</updated><content type="html">&lt;H2&gt;&lt;IMG class=nearGraphic title="Hey, Scripting Guy! Question" height=34 alt="Hey, Scripting Guy! Question" src="http://img.microsoft.com/library/media/1033/technet/images/scriptcenter/qanda/q-sm.jpg" width=34 align=left border=0 mce_src="http://img.microsoft.com/library/media/1033/technet/images/scriptcenter/qanda/q-sm.jpg"&gt; &lt;/H2&gt;
&lt;P&gt;Hey, Scripting Guy! In a previous column, you told us a couple different ways to save Excel spreadsheets. In that column, you said that one of the things you could do with the SaveAs method was password-protect a spreadsheet. However, you didn’t show us an example of this. How can I use a script to password-protect a spreadsheet?&lt;BR&gt;&lt;BR&gt;-- MC&lt;/P&gt;&lt;IMG height=5 alt=Spacer src="http://img.microsoft.com/library/media/1033/technet/images/scriptcenter/spacer.gif" width=5 border=0 mce_src="http://img.microsoft.com/library/media/1033/technet/images/scriptcenter/spacer.gif"&gt;&lt;IMG class=nearGraphic title="Hey, Scripting Guy! Answer" height=34 alt="Hey, Scripting Guy! Answer" src="http://img.microsoft.com/library/media/1033/technet/images/scriptcenter/qanda/a-sm.jpg" width=34 align=left border=0 mce_src="http://img.microsoft.com/library/media/1033/technet/images/scriptcenter/qanda/a-sm.jpg"&gt;&lt;A href="http://go.microsoft.com/fwlink/?linkid=68779&amp;amp;clcid=0x409" mce_href="http://go.microsoft.com/fwlink/?linkid=68779&amp;amp;clcid=0x409"&gt;&lt;/A&gt; 
&lt;P&gt;Hey, MC. Well, as we hinted at in the &lt;A href="http://www.microsoft.com/technet/scriptcenter/resources/qanda/jan05/hey0131.mspx" mce_href="http://www.microsoft.com/technet/scriptcenter/resources/qanda/jan05/hey0131.mspx"&gt;previous column&lt;/A&gt; you mentioned, any time you call the SaveAs method you can include password-protection as an optional parameter. To demonstrate, here’s a script that creates a new worksheet, writes the current date and time in cell A1, and then saves the worksheet as C:\Scripts\Test.xls. On top of that, it password-protects the spreadsheet, giving it the password &lt;B&gt;%reTG54w&lt;/B&gt;:&lt;/P&gt;&lt;PRE style="BORDER-RIGHT: #999999 1px dashed; PADDING-RIGHT: 5px; BORDER-TOP: #999999 1px dashed; PADDING-LEFT: 5px; FONT-SIZE: 12px; PADDING-BOTTOM: 5px; OVERFLOW: auto; BORDER-LEFT: #999999 1px dashed; WIDTH: 100%; COLOR: #000000; LINE-HEIGHT: 14px; PADDING-TOP: 5px; BORDER-BOTTOM: #999999 1px dashed; FONT-FAMILY: Andale Mono, Lucida Console, Monaco, fixed, monospace; BACKGROUND-COLOR: #eee"&gt;Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
objExcel.DisplayAlerts = FALSE

Set objWorkbook = objExcel.Workbooks.Add
Set objWorksheet = objWorkbook.Worksheets(1)

objWorksheet.Cells(1, 1).Value = Now
objWorkbook.SaveAs "C:\Scripts\Test.xls",,"%reTG54w"
objExcel.Quit
&lt;/PRE&gt;
&lt;P&gt;If you’ve done any scripting with Microsoft Excel, this is about as simple a script as you’ll ever write. The only “gotcha” to be aware of occurs in this line of code, where you actually save the file and password-protect it:&lt;/P&gt;&lt;PRE style="BORDER-RIGHT: #999999 1px dashed; PADDING-RIGHT: 5px; BORDER-TOP: #999999 1px dashed; PADDING-LEFT: 5px; FONT-SIZE: 12px; PADDING-BOTTOM: 5px; OVERFLOW: auto; BORDER-LEFT: #999999 1px dashed; WIDTH: 100%; COLOR: #000000; LINE-HEIGHT: 14px; PADDING-TOP: 5px; BORDER-BOTTOM: #999999 1px dashed; FONT-FAMILY: Andale Mono, Lucida Console, Monaco, fixed, monospace; BACKGROUND-COLOR: #eee"&gt;objWorkbook.SaveAs "C:\Scripts\Test.xls",,"%reTG54w"
&lt;/PRE&gt;
&lt;P&gt;Admittedly, there’s nothing fancy about this line of code; you just have to make sure the password (&lt;B&gt;%reTG54w&lt;/B&gt;) is the &lt;I&gt;third&lt;/I&gt; parameter passed to the SaveAs method. The first parameter is, of course, the file name. The second parameter is the file format. Because we’re using the default format, we don’t need to set a value for the second parameter; however, we &lt;I&gt;do&lt;/I&gt; need to include a placeholder for that parameter. That’s what the back-to-back commas (,,) are for: they simply indicate that the value for the second parameter would go here if we actually &lt;I&gt;had&lt;/I&gt; a value for the second parameter. By including this placeholder, the password becomes the third parameter, which is exactly what we want.&lt;/P&gt;
&lt;P&gt;After you run this script, try to open the file C:\Scripts\Test.xls; you’ll be prompted to enter the password before the file will actually be opened. Incidentally, this will happen even if you try opening the file using a script. (Sorry, but using a script won’t allow you to bypass the password protection.) But can’t you specify the password when you open the file? Of course you can; that’s what happens with this script:&lt;/P&gt;&lt;PRE style="BORDER-RIGHT: #999999 1px dashed; PADDING-RIGHT: 5px; BORDER-TOP: #999999 1px dashed; PADDING-LEFT: 5px; FONT-SIZE: 12px; PADDING-BOTTOM: 5px; OVERFLOW: auto; BORDER-LEFT: #999999 1px dashed; WIDTH: 100%; COLOR: #000000; LINE-HEIGHT: 14px; PADDING-TOP: 5px; BORDER-BOTTOM: #999999 1px dashed; FONT-FAMILY: Andale Mono, Lucida Console, Monaco, fixed, monospace; BACKGROUND-COLOR: #eee"&gt;Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
objExcel.DisplayAlerts = FALSE

Set objWorkbook = objExcel.Workbooks.Open("C:\Scripts\Test.xls",,,,"%reTG54w")
&lt;/PRE&gt;
&lt;P&gt;Note that when opening a spreadsheet the password has to be the &lt;I&gt;fifth&lt;/I&gt; parameter; thus we have the file name, three placeholders, and then the password. This can be a little confusing, to say the least, but here’s a rule of thumb: just put in one more comma than you have placeholders. In this example, we have three placeholders, so we insert four commas. If we had nine placeholders, we’d insert ten commas. And so on.&lt;/P&gt;
&lt;P&gt;But what if you decide later on to &lt;I&gt;remove&lt;/I&gt; the password? No problem: simply open the file and set the value of the &lt;B&gt;Password&lt;/B&gt; property to an empty string. Here’s a script that does just that: it opens the spreadsheet, removes the password, and then uses the SaveAs method to re-save the file. After running this script, try to open this spreadsheet from within Windows Explorer; you should be able to do so without being prompted for a password.&lt;/P&gt;&lt;PRE style="BORDER-RIGHT: #999999 1px dashed; PADDING-RIGHT: 5px; BORDER-TOP: #999999 1px dashed; PADDING-LEFT: 5px; FONT-SIZE: 12px; PADDING-BOTTOM: 5px; OVERFLOW: auto; BORDER-LEFT: #999999 1px dashed; WIDTH: 100%; COLOR: #000000; LINE-HEIGHT: 14px; PADDING-TOP: 5px; BORDER-BOTTOM: #999999 1px dashed; FONT-FAMILY: Andale Mono, Lucida Console, Monaco, fixed, monospace; BACKGROUND-COLOR: #eee"&gt;Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
objExcel.DisplayAlerts = FALSE

Set objWorkbook = objExcel.Workbooks.Open("C:\Scripts\Test.xls",,,,"%reTG54w")
Set objWorksheet = objWorkbook.Worksheets(1)

objWorkbook.Password = ""

objWorkbook.SaveAs "C:\Scripts\Test.xls"
objExcel.Quit
&lt;/PRE&gt;
&lt;P&gt;What if you didn’t want to remove the password, but merely wanted to change it? In that case, just set the Password property to the new password rather than to an empty string. And before you ask, yes, we could have used the Save method here rather than SaveAs. We stuck with SaveAs simply to be consistent with the previous script. We’re also aware that “Consistency is the hobgoblin of little minds.” But, hey, what size minds would you &lt;I&gt;expect&lt;/I&gt; the Scripting Guys to have?!?&lt;/P&gt;&lt;img src="http://blogs.technet.com/aggbug.aspx?PostID=3163177" width="1" height="1"&gt;</content><author><name>TharikaC</name><uri>http://blogs.technet.com/members/TharikaC.aspx</uri></author><category term="Microsoft Office" scheme="http://blogs.technet.com/scriptingguy/archive/tags/Microsoft+Office/default.aspx" /></entry><entry><title>How Can I Boldface a Specific Word Throughout a Microsoft Word Document?</title><link rel="alternate" type="text/html" href="http://blogs.technet.com/scriptingguy/archive/2008/12/03/how-can-i-boldface-a-specific-word-throughout-a-microsoft-word-document.aspx" /><id>http://blogs.technet.com/scriptingguy/archive/2008/12/03/how-can-i-boldface-a-specific-word-throughout-a-microsoft-word-document.aspx</id><published>2008-12-03T17:13:00Z</published><updated>2008-12-03T17:13:00Z</updated><content type="html">&lt;H2&gt;&lt;IMG class=nearGraphic title="Hey, Scripting Guy! Question" height=34 alt="Hey, Scripting Guy! Question" src="http://img.microsoft.com/library/media/1033/technet/images/scriptcenter/qanda/q-sm.jpg" width=34 align=left border=0 mce_src="http://img.microsoft.com/library/media/1033/technet/images/scriptcenter/qanda/q-sm.jpg"&gt; &lt;/H2&gt;
&lt;P&gt;Hey, Scripting Guy! How can I boldface a specific word throughout a Microsoft Word document?&lt;BR&gt;&lt;BR&gt;-- SB&lt;/P&gt;&lt;IMG height=5 alt=Spacer src="http://img.microsoft.com/library/media/1033/technet/images/scriptcenter/spacer.gif" width=5 border=0 mce_src="http://img.microsoft.com/library/media/1033/technet/images/scriptcenter/spacer.gif"&gt;&lt;IMG class=nearGraphic title="Hey, Scripting Guy! Answer" height=34 alt="Hey, Scripting Guy! Answer" src="http://img.microsoft.com/library/media/1033/technet/images/scriptcenter/qanda/a-sm.jpg" width=34 align=left border=0 mce_src="http://img.microsoft.com/library/media/1033/technet/images/scriptcenter/qanda/a-sm.jpg"&gt;&lt;A href="http://go.microsoft.com/fwlink/?linkid=68779&amp;amp;clcid=0x409" mce_href="http://go.microsoft.com/fwlink/?linkid=68779&amp;amp;clcid=0x409"&gt;&lt;/A&gt; 
&lt;P&gt;Hey, SB. You’ll probably find this hard to believe, but when you work with scripts and scripting all day you tend to get a little jaded. You say you have a script that automatically backs up and clears all the event logs on all your computers? How nice. You have a script that monitors folders and lets you know when new files are added? Been there, done that. You have a script that can bring the dead back to life? Hey, who doesn’t?&lt;/P&gt;
&lt;P&gt;For some reason, though, SB, your question piqued our interest. Maybe it’s because we’re always looking for a script that uses Microsoft Word; scripting with Word is actually kind of fun. Or maybe it’s because this request offered a bit of a challenge: although we were pretty sure that we could write a script that would boldface specific words in a document we’d never actually tried it. Or maybe it’s because all the dead people we brought back to life have been pretty insistent that we go through &lt;I&gt;their&lt;/I&gt; documents and boldface specific words. You know, things like the phrase &lt;B&gt;I am &lt;/B&gt;&lt;B&gt;&lt;I&gt;too&lt;/I&gt;&lt;/B&gt;&lt;B&gt; alive&lt;/B&gt;.&lt;/P&gt;
&lt;P&gt;Oh, well. Regardless of what motivated us, here’s what we came up with:&lt;/P&gt;&lt;PRE style="BORDER-RIGHT: #999999 1px dashed; PADDING-RIGHT: 5px; BORDER-TOP: #999999 1px dashed; PADDING-LEFT: 5px; FONT-SIZE: 12px; PADDING-BOTTOM: 5px; OVERFLOW: auto; BORDER-LEFT: #999999 1px dashed; WIDTH: 100%; COLOR: #000000; LINE-HEIGHT: 14px; PADDING-TOP: 5px; BORDER-BOTTOM: #999999 1px dashed; FONT-FAMILY: Andale Mono, Lucida Console, Monaco, fixed, monospace; BACKGROUND-COLOR: #eee"&gt;Const wdReplaceAll  = 2

Set objWord = CreateObject("Word.Application")
objWord.Visible = True

Set objDoc = objWord.Documents.Open("C:\Scripts\Test.doc")
Set objSelection = objWord.Selection

objSelection.Find.Text = "Fabrikam"
objSelection.Find.Forward = TRUE
objSelection.Find.MatchWholeWord = TRUE

objSelection.Find.Replacement.Font.Bold = True

objSelection.Find.Execute ,,,,,,,,,,wdReplaceAll
&lt;/PRE&gt;
&lt;P&gt;As you can see, not only was this a fun script to write but - as an added bonus - it was an easy script to write, too. After defining a constant named wdReplaceAll (we’ll talk about that in a moment), we create an instance of the &lt;B&gt;Word.Application&lt;/B&gt; object and then set the &lt;B&gt;Visible&lt;/B&gt; property to True; this gives us an instance of Microsoft Word that we can see on screen. We use the &lt;B&gt;Open&lt;/B&gt; method to open the document C:\Scripts\Test.doc, then create an instance of the Word &lt;B&gt;Selection&lt;/B&gt; object (which, by default, positions the cursor at the beginning of the document).&lt;/P&gt;
&lt;P&gt;Now we’re ready to roll. What we want to do is boldface any instances of the word&lt;I&gt; Fabrikam&lt;/I&gt;; hence we set the value of the &lt;B&gt;Find&lt;/B&gt; object’s &lt;B&gt;Text&lt;/B&gt; property to “Fabrikam” (in other words, that’s what we’re searching for). Next we set the &lt;B&gt;Forward&lt;/B&gt; property to True; this ensures that our search will start at the beginning of the document and end at, well, the end of the document. Finally, we set the &lt;B&gt;MatchWholeWord&lt;/B&gt; property to True; we do this just in case there happens to be some crazy construction like FabrikamCorporation in the document. If MatchWholeWord was set to False, then &lt;I&gt;any&lt;/I&gt; instance of the string Fabrikam would be boldfaced. That would give us something similar to this: &lt;B&gt;Fabrikam&lt;/B&gt;Corporation. Which we probably don’t want.&lt;/P&gt;
&lt;P&gt;
&lt;TABLE class=dataTable id=ELE cellSpacing=0 cellPadding=0 class="dataTable"&gt;
&lt;THEAD&gt;&lt;/THEAD&gt;
&lt;TBODY&gt;
&lt;TR class=record vAlign=top&gt;
&lt;TD class="" style="BORDER-RIGHT: #cccccc 1px solid"&gt;
&lt;P class=lastInCell&gt;&lt;B&gt;Note&lt;/B&gt;. Yes, we &lt;I&gt;are&lt;/I&gt; kind of zipping through the explanation here, aren’t we? But that’s OK: after all, we have an &lt;A href="http://www.microsoft.com/technet/scriptcenter/resources/officetips/may05/tips0512.mspx" mce_href="http://www.microsoft.com/technet/scriptcenter/resources/officetips/may05/tips0512.mspx"&gt;&lt;B&gt;Office Space article&lt;/B&gt;&lt;/A&gt; that discusses the process of finding and replacing text in more detail.&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/P&gt;
&lt;DIV class=dataTableBottomMargin&gt;&lt;/DIV&gt;
&lt;P&gt;All that gives us a mechanism for finding all instances of the word Fabrikam. But once we find them how do we boldface each of those instances? Here’s how:&lt;/P&gt;&lt;PRE style="BORDER-RIGHT: #999999 1px dashed; PADDING-RIGHT: 5px; BORDER-TOP: #999999 1px dashed; PADDING-LEFT: 5px; FONT-SIZE: 12px; PADDING-BOTTOM: 5px; OVERFLOW: auto; BORDER-LEFT: #999999 1px dashed; WIDTH: 100%; COLOR: #000000; LINE-HEIGHT: 14px; PADDING-TOP: 5px; BORDER-BOTTOM: #999999 1px dashed; FONT-FAMILY: Andale Mono, Lucida Console, Monaco, fixed, monospace; BACKGROUND-COLOR: #eee"&gt;objSelection.Find.Replacement.Font.Bold = True
&lt;/PRE&gt;
&lt;P&gt;Yes, it’s that easy. Here we use the &lt;B&gt;Replacement&lt;/B&gt; object (a child object of the Find object) and specify that the &lt;B&gt;Font.Bold&lt;/B&gt; property should be True. That’s all we have to do. If we wanted to un-boldface each instance of Fabrikam we would set the Font.Bold property to False. Needless to say, we could specify new replacement text, a new replacement font size, pretty much anything we want.&lt;/P&gt;
&lt;P&gt;See why we like scripting with Microsoft Word so much?&lt;/P&gt;
&lt;P&gt;After configuring the replacement object we then call the &lt;B&gt;Execute&lt;/B&gt; method to start the find-and-replace operation:&lt;/P&gt;&lt;PRE style="BORDER-RIGHT: #999999 1px dashed; PADDING-RIGHT: 5px; BORDER-TOP: #999999 1px dashed; PADDING-LEFT: 5px; FONT-SIZE: 12px; PADDING-BOTTOM: 5px; OVERFLOW: auto; BORDER-LEFT: #999999 1px dashed; WIDTH: 100%; COLOR: #000000; LINE-HEIGHT: 14px; PADDING-TOP: 5px; BORDER-BOTTOM: #999999 1px dashed; FONT-FAMILY: Andale Mono, Lucida Console, Monaco, fixed, monospace; BACKGROUND-COLOR: #eee"&gt;objSelection.Find.Execute ,,,,,,,,,,wdReplaceAll
&lt;/PRE&gt;
&lt;P&gt;And, no, we didn’t fall asleep with our finger on the comma key. Well, not this time anyway. As it turns out, there are &lt;I&gt;tons&lt;/I&gt; of optional parameters available for the Execute method. For this script we didn’t care about any of those. However, because parameters have to be specified in order, we couldn’t just do something like this:&lt;/P&gt;&lt;PRE class=codeSample&gt;objSelection.Find.Execute wdReplaceAll
&lt;/PRE&gt;
&lt;P&gt;That would make the constant ReplaceAll (which tells Word we want to replace all instances of our search text) the first parameter passed to the Execute method, which would cause the script to fail. Hence all the “empty” parameters represented by the commas. And don’t worry: if that doesn’t make any sense to you, take a look at the documentation for the &lt;A href="http://msdn.microsoft.com/en-us/library/aa142035(office.10).aspx" target=_blank mce_href="http://msdn.microsoft.com/en-us/library/aa142035(office.10).aspx"&gt;&lt;B&gt;Execute&lt;/B&gt;&lt;/A&gt; method and you’ll see a list of all the parameters we skipped.&lt;/P&gt;
&lt;P&gt;OK. That was fun, wasn’t it? In fact, we’d love to stay and chat about it some more, but Abe Lincoln and Genghis Khan are both out of iced tea. You know, writing a script to bring the dead back to life really wasn’t all it’s cracked up to be ….&lt;/P&gt;&lt;img src="http://blogs.technet.com/aggbug.aspx?PostID=3163176" width="1" height="1"&gt;</content><author><name>TharikaC</name><uri>http://blogs.technet.com/members/TharikaC.aspx</uri></author><category term="Microsoft Office" scheme="http://blogs.technet.com/scriptingguy/archive/tags/Microsoft+Office/default.aspx" /></entry><entry><title>How Can I Tell the Status of Services on a Computer?</title><link rel="alternate" type="text/html" href="http://blogs.technet.com/scriptingguy/archive/2008/02/12/how-can-i-tell-the-status-of-services-on-a-computer.aspx" /><id>http://blogs.technet.com/scriptingguy/archive/2008/02/12/how-can-i-tell-the-status-of-services-on-a-computer.aspx</id><published>2008-02-12T23:31:00Z</published><updated>2008-02-12T23:31:00Z</updated><content type="html">&lt;P&gt;Hey, Scripting Guy! I’m wondering if you can help me before I tear &lt;I&gt;all&lt;/I&gt; my hair out. I’m trying to write a script using WMI that checks for a few core services such as antivirus, remote desktop software, and the like. The intention is to bolt this information onto another one of your (brilliant little) scripts I use to e-mail this information if certain things aren’t installed.&lt;/P&gt;
&lt;P&gt;I’ve found a really convoluted script somewhere on the Internet that enumerates all services along with service users and a count of services at the end, but this is really overkill. I just want an “If exist” type script for a handful of services, which we can then run on all our PCs!&lt;/P&gt;
&lt;P&gt;- RT&lt;/P&gt;&lt;IMG height=5 alt=Spacer src="http://img.microsoft.com/library/media/1033/technet/images/scriptcenter/spacer.gif" width=5 border=0 mce_src="http://img.microsoft.com/library/media/1033/technet/images/scriptcenter/spacer.gif"&gt;&lt;IMG class=nearGraphic title="Hey, Scripting Guy! Answer" height=34 alt="Hey, Scripting Guy! Answer" src="http://img.microsoft.com/library/media/1033/technet/images/scriptcenter/qanda/a-sm.jpg" width=34 align=left border=0 mce_src="http://img.microsoft.com/library/media/1033/technet/images/scriptcenter/qanda/a-sm.jpg"&gt; 
&lt;P&gt;Hi RT,&lt;/P&gt;
&lt;P&gt;&lt;A href="http://www.microsoft.com/technet/scriptcenter/guide/sas_ser_vtli.mspx?mfr=true" target=_blank mce_href="http://www.microsoft.com/technet/scriptcenter/guide/sas_ser_vtli.mspx?mfr=true"&gt;The Scripting Guide&lt;/A&gt; talks about listing the status of services, and probably is the kind of script you were railing against. People sometimes make a big deal about Windows PowerShell "one liners," but you can certainly use a more compact syntax within VBScript as well. The thing is, no one ever did. I actually came up with this type of syntax when I was writing the Microsoft Press book, &lt;A href="http://www.microsoft.com/MSPress/books/authors/auth8853.aspx" target=_blank mce_href="http://www.microsoft.com/MSPress/books/authors/auth8853.aspx"&gt;Microsoft Windows Scripting with WMI: Self-Paced Learning Guide&lt;/A&gt;. I also used it quite a bit when I was writing scripts for the &lt;A href="http://www.microsoft.com/learning/en/us/books/9536.aspx" target=_blank mce_href="http://www.microsoft.com/learning/en/us/books/9536.aspx"&gt;Windows Vista Resource Kit&lt;/A&gt; (First Edition). In that book, I was interested in printing out the user name and the computer name. Because the script might be run remotely, I could not use any of the properties from the WshShell object. You can also use something such as this VBScript to solve your problem.&lt;/P&gt;&lt;img src="http://blogs.technet.com/aggbug.aspx?PostID=3181325" width="1" height="1"&gt;</content><author><name>TharikaC</name><uri>http://blogs.technet.com/members/TharikaC.aspx</uri></author><category term="Administration" scheme="http://blogs.technet.com/scriptingguy/archive/tags/Administration/default.aspx" /></entry></feed>