Script Cmdlet to Empty the Recycle bin

September 3, 2008 – 12:20 pm

 

This weekend I needed to delete some files so I could defrag my hard drive. I only had 2% free and needed to get past that 15% threshold so it would work efficiently. I proceeded to do some house cleaning and started deleting old unneeded files. When I was done I had to empty the recycle bin. Yes I know I could have just done Shift-Delete to permanently delete the files, but I usually don’t remember and this is a little safer in case I do delete something I need. So as with everything else I do, I immediately though how could I do this in PowerShell. Deleting the files was the easy part. Emptying the Recycle Bin, I had to research.

After some research I found out there was a Windows API function, SHEmptyRecycleBin, that does exactly what I needed.  Of course, since it wasn’t a .Net function I wasn’t sure how I was going to use it in PowerShell. Then I realized that I had seen an example by jaykul where he imbedded C# code into a PowerShell script. He pretty much did all the work. I just tweaked it to fit my situation. 

This script cmdlet will only work in the version 2 CTP, but if you are driven to get it to work in version 1 I think there are ways to get it done.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • e-mail
  • Live
  • Slashdot
  • StumbleUpon
  • Technorati
  • TwitThis

Check out the PowerScripting Podcast Live Video Feed

August 21, 2008 – 10:58 pm

 

Watch the guys in action

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • e-mail
  • Live
  • Slashdot
  • StumbleUpon
  • Technorati
  • TwitThis

The Meaning of My Blog Name

April 25, 2008 – 9:00 am

If you are reading my blog then you might be wondering what the importance of the name of String Theory. Before you guess, No I am not talking about strings in the computing sense. I am talking about strings in the Physics’ Unified Field Theory sense.  I majored in Physics in college and had I not decided to venture into the computing field I probably would have set my sights on the String Theory for study.

If you have no idea what the String Theory, is watch the video below from  http://www.ted.com where the well-known physicist Brian Greene gives a nontechnical, easy to understand explanation of String Theory.


Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • e-mail
  • Live
  • Slashdot
  • StumbleUpon
  • Technorati
  • TwitThis

My Technical Library

April 22, 2008 – 7:25 pm

 

In response to Steven Murawski’s blog post, here are the books currently in my library in no particular order.

Web Database Applications with PHP & MySQL, 2nd Edition
by Hugh E. Williams

Read more about this book…
Professional VB 2005 (Programmer to Programmer)
by Bill Evjen, Billy Hollis, Rockford Lhotka, Tim McCarthy, Rama Ramachandran, Kent Sharkey, Bill Sheldon

Read more about this book…
XML 1.1 Bible
by Elliotte Rusty Harold

Read more about this book…
Mastering Regular Expressions
by Jeffrey Friedl

Read more about this book…
Microsoft Operations Manager 2005 Unleashed (MOM): With A Preview of Operations Manager 2007 (Unleashed)
by Kerrie Meyler, Cameron Fuller

Read more about this book…
Official Samba-3 HOWTO and Reference Guide, The (2nd Edition) (Bruce Perens’ Open Source Series)
by John H. Terpstra, Jelmer R. Vernooij

Read more about this book…
Red Hat Fedora Core 6 Unleashed
by Paul Hudson, Andrew Hudson

Read more about this book…
Windows PowerShell in Action
by Bruce Payette

Read more about this book…
Windows PowerShell Cookbook: for Windows, Exchange 2007, and MOM V3
by Lee Holmes

Read more about this book…
Code Complete: A Practical Handbook of Software Construction
by Steve McConnell

Read more about this book…
Professional C# 2005 (Wrox Professional Guides)
by Christian Nagel, Bill Evjen, Jay Glynn, Morgan Skinner, Karli Watson, Allen Jones

Read more about this book…
Professional ASP.NET 2.0 Special Edition (Wrox Professional Guides)
by Bill Evjen, Scott Hanselman, Devin Rader, Farhan Muhammad, Srinivasa Sivakumar

Read more about this book…
Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • e-mail
  • Live
  • Slashdot
  • StumbleUpon
  • Technorati
  • TwitThis

XenServer VM PowerShell Cmdlet

April 16, 2008 – 8:21 am

 

Citrix has released some cmdlets for use with their XenServer virtualization product. No where near as powerful as VMWare’s cmdlets, but at least its a start. You can check them out and get more information at the URL below.

http://community.citrix.com/display/cdn/XenServe+VM+PowerShell+CmdLet

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • e-mail
  • Live
  • Slashdot
  • StumbleUpon
  • Technorati
  • TwitThis

Setting PowerShell Execution Policy Remotely

April 13, 2008 – 8:20 pm

 

So, you have seen the light and want to use PowerShell for all your daily tasks. By default the PowerShell execution policy is set to Restricted, meaning that PowerShell does not load configuration files and does not run scripts. You will have to change the execution policy for any machine you want to run scripts on. For a large number of machines that would be a pain so I will give you three different methods for setting the execution policy for PowerShell.

Read the rest of this entry »
Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • e-mail
  • Live
  • Slashdot
  • StumbleUpon
  • Technorati
  • TwitThis

Bitwise Operators

April 7, 2008 – 7:59 pm

Today we will take a look at the Bitwise Operators in Windows PowerShell and how you can manipulate them for your needs.  First we will get some insight into the enumeration. Let’s begin by looking at the values for the [System.IO.FileAttributes] enumeration which provides attributes for files and directories.

As you can see from the table below each member corresponds to a different attribute that you can apply to files and directories. Some of these are common values that you see daily such as ReadOnly and Hidden, but others are not so common. I won’t go into the meaning of each of these, but if you are interested you can review them on the MSDN site here. Each value, in decimal, is a power of 2 greater than the previous value. To add attributes you add the binary value. So the attributes for a file that is ReadOnly and Hidden would be "1" + "10" = "11" or 3.

An interesting note is that given any enumeration value there can be only ONE possible combination of values that will add up to the enumeration value.

Member Name Binary Value Decimal Value
ReadOnly 1 1
Hidden 10 2
System 100 4
Directory 10000 16
Archive 100000 32
Device 1000000 64
Normal 10000000 128
Temporary 100000000 256
SparseFile 1000000000 512
Compressed 10000000000 2048
Offline 100000000000 4096
Encrypted 1000000000000 16384

So. Enough for the theory. Let’s have fun.

Determining if an attribute is set

The first task we want to accomplish is checking to see whether an attribute is set on a specified file. In this case we are using a file called "test.txt" with the Hidden and ReadOnly attributes set. First we get the file information and attributes. Then we use the -band bitwise operator to compare the objects.

$File = Get-ChildItem .\test.txt -Force
$File.Attributes
if ( $File.Attributes -band [System.IO.FileAttributes]::Hidden )
{ Write-Host "Hidden Attribute Set" }

With the "-band" operator each binary value is compared and in each position if a 1 is present in both numbers then a 1 is returned. If not, a 0 is returned. So for our example "11 -band 10" is equal to 10 because a 1 is in the second position in both numbers. The way that enumerations are setup dictate two possible values for this comparison. Either the returned value is equal to the attribute being compared or 0, indicating that the attribute is not set.

Setting an attribute

The next task we want to perform is setting an attribute. We are going to use the same file we used in the previous example and set the System attribute. For this task we are going to use the -bor operator.

$File = Get-ChildItem .\test.txt -Force
$File.Attributes
$File.Attributes = ( $File.Attributes -bor [System.IO.FileAttributes]::System )
$File.Attributes

Here, by using the "-bor" operator each binary value is compared and in each position if there is a one present in either number then a one is returned for that position. It’s like a light switch. If it is turned on in either number then it is turned on in the resulting output.  Using this analogy we are flipping on the switch that corresponds to the System attribute.

Removing an attribute

The last task is removing an attribute. For this example we are using the "-bxor" operator. This operator is like a two-way light switch. If you turn on one light switch, the light is on. If you then turn on the other switch, the light turns off.  So you can probably see the issue. If you run the following script a couple of times on the same file you will notice that the System attributes gets turned on or off each time you run it.

$File = Get-ChildItem .\test.txt -Force
$File.Attributes
$File.Attributes = ( $File.Attributes -bxor [System.IO.FileAttributes]::System )
$File.Attributes

This is good if you want to set an attribute to the opposite of the current setting. However if you want to just remove an attribute you will need the following script.


$File = Get-ChildItem .\test.txt -Force
$File.Attributes
if (  $File.Attributes -band [System.IO.FileAttributes]::System)
{ $File.Attributes = ( $File.Attributes -bxor [System.IO.FileAttributes]::System )  } $File.Attributes

We use the "-band" operator to make sure the option is set and then unset it using the "-bxor" operator.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • e-mail
  • Live
  • Slashdot
  • StumbleUpon
  • Technorati
  • TwitThis

PowerScripting Podcast - Episode 22 “The One About Brandon”

April 3, 2008 – 7:12 pm

Hal and Jonathan have done it again.
Today we’ll bring you the second half of our interview with Jeffrey Snover. We think this part is even better than the first half. We’ve also got some news, resources, and a bunch of PowerShell tips for you.
Go pick up the podcast at http://powerscripting.wordpress.com/.
Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • e-mail
  • Live
  • Slashdot
  • StumbleUpon
  • Technorati
  • TwitThis