Installing PowerShell on Windows XP, and Copying Files

CraigLieb

Summary: Learn how to install Windows PowerShell 2.0 on Windows XP, and how to distribute files between two folders.

 

In this Post:

 

Where Can I Download Windows PowerShell 2.0 for Windows XP?

Hey, Scripting Guy! Question

Hey, Scripting Guy! I’m new to Windows PowerShell. Currently, I am using Windows XP. I have had no luck with downloading Windows PowerShell for Windows XP yet. Could you please give me a specific link so that I can download it? Many thanks.

— KB

 

Hey, Scripting Guy! Answer

Hello KB,

Microsoft Scripting Guy Ed Wilson is here. I am sorry you are having a problem downloading and installing Windows PowerShell 2.0 for Windows XP. This is the download page for Windows PowerShell 2.0. Unfortunately, it is not called “Download page for Windows PowerShell 2.0”; it is called TechNet Support Article 968929, and the title on the page says Windows Management Framework. I guess the only clue is the stuff in the parentheses where it says Windows PowerShell 2.0, WinRM 2.0, and Bits 4.0.

After you get to the download page, you will need to go all the way down to the bottom of the page. This is because this page contains links to all of the different packages that are required for everything from Windows Server 2008 to Windows Vista 64-bit. The package you want is a bit confusing, because it is for both Windows XP and Windows embedded. (To be honest, when the Scripting Wife asked me if it was the right package, I was not entirely certain. I just told her to download it and to try it, hoping that it would include a version check and that the installation would fail, if it were the wrong package). I have included a picture that points to the exact package you want.

Image of download link location on Windows PowerShell 2.0 download page

My wife, the Scripting Wife, also had a bit of difficulty downloading and installing Windows PowerShell 2.0. You may want to take a look at her article, as it goes through the process step by step.

After you get it up and running, you may want to look at all the Scripting Wife articles, because she details her experience getting ready for the 2010 Scripting Games. She was an accountant, and though she is knowledgeable about computers, she is not a computer professional. Start at the beginning of the articles and read them forward. They kind of become her scripting diary.

Hope this helps.


How to Distribute Files Between FoldersHey, Scripting Guy! Question

Hey, Scripting Guy! Hello, maybe you can help me out. I am desperate. This is probably a stupid question, but I am currently trying to write a script that copies files from one source directory based on the number of files in the source to two different directory destinations. For example, if I have eight files in the source directory (A), I want to copy four of the eight files in destination directory (B) and copy the other four files in destination directory (C). I cannot seem to figure this out anywhere! I currently have this as my script.

CopyFiles_DoesNotWork.ps1

# Define source directory and destination directories to copy files:

$SQL01="C:\SQL01Source\"

$DESTSQL02="C:\SQL02\"

$DESTSQL03="C:\SQL03\"

# Get directory items number and divide number received:

(get-childitem $SQL01).count /2

#Copy items to destination folders:

copy-item $SQL01 $DESTSQL02 -recurse

I am stuck! Your help would be greatly appreciated!

— JT

 

Hey, Scripting Guy! Answer

Hello JT,

The first thing I needed to do was to create a couple of folders (a, b, and c off the root). After I did that, I needed to create eight files. I used this line of code to create my eight files:

1..8 | % {New-Item -Path "c:\a" -ItemType file -Name "file$_" -Value "file$_"}

After I created my test environment, it was a simple matter of writing a quick script to copy the files. It is significant that you said you wanted to copy the files, instead of moving them. With a copy, you will still have your eight original files in the original location, and in your two destination folders, you will have a copy of the first four files, and a copy of the second four files in the second destination folder. The trick to accomplishing your task is to add a bit of logic to the code so that it will know how many files have been copied to the first destination folder. When half of the files have been copied to the first destination, the script needs to switch to using the second destination folder. Whenever I have a script that needs to make a decision, I use a decision type of code structure—in this example, it is an If/Else type of code.

JT, I wrote the DivideAndCopyFiles.ps1 script to illustrate using this type of technique in your situation. The complete script appears here.

DivideAndCopyFiles.ps1

$source = "C:\a"

$destination1 = "c:\b"

$destination2 = "C:\c"

$i = 1

$files = Get-ChildItem -Path $source -Recurse

Foreach($file in $files)

{

if($i -le $files.count/2)

{ Copy-Item -Path $file.fullname -Destination $destination1 }

Else

{ Copy-Item -Path $file.fullname -Destination $destination2 }

$i++

}

In the first section of the script, I declare a bunch of variables. These are pretty self-explanatory, except for the $i variable. The $i variable is a counter variable that is used to keep track of our progress in copying the files. It is important to initialize it to the value of 1, because if it were not initialized and the script were run a second time in the Windows PowerShell ISE, the script would not work properly because of the counter value being greater than the number of files in the collection. The variable portion of the script is shown here:

$source = "C:\a"

$destination1 = "c:\b"

$destination2 = "C:\c"

$i = 1

Next, I gather up all of the files from the source folder. To do this, I use the Get-ChildItem cmdlet. I store the returned objects in the $files variable. The recurse parameter is used to tell the Get-ChildItem cmdlet to retrieve items that may reside in a subdirectory. This command appears here:

$files = Get-ChildItem -Path $source -Recurse

Next, I use the foreach statement to walk through the collection of objects that are stored in the $files variable. The $file variable is used to keep my place in the collection of files. Following the foreach statement, I open the script block by using a curly bracket (brace). This portion of the script appears here:

Foreach($file in $files)

{

The next thing I need to do is to add a bit of logic. The If statement is used to evaluate a condition. The condition I am interested in is if the value of the variable $i is less than or equal to half of the total number of files in the $files collection. Luckily, the count property exists to tell me how many files I have to work with. As long as the number of files is less than half, I copy the files to the path stored in the $destination1 variable. If the value of the $i variable is greater than half the number of files in the $files variable, I copy them to the path stored in the $destination2 variable.

When I am copying files, I need to supply the path parameter with the path to the file that is to be copied. The fullname property of a fileinfo object contains the file name, as well as the path to the file. When I am using the Copy-Item cmdlet, I do not need to specify a file name on the destination side of things because the cmdlet is smart enough to obtain that. All I need to do is to give it the destination folder. This portion of the script appears here:

if($i -le $files.count/2)

{ Copy-Item -Path $file.fullname -Destination $destination1 }

Else

{ Copy-Item -Path $file.fullname -Destination $destination2 }

Finally, after copying a file, I increment the value of the $i variable and close out the script block:

$i++

}

Well, this concludes another edition of Quick-Hits Friday. Join me tomorrow for the Weekend Scripter as I delve into the mysteries of using the Write-Progress cmdlet.

I would love you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

 

Ed Wilson, Microsoft Script ing Guy

 



0 comments

Discussion is closed.

Feedback usabilla icon