Installing PowerShell on Windows XP, and Copying Files

Installing PowerShell on Windows XP, and Copying Files

  • Comments 11
  • Likes

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 Scripting Guy

 



Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Thanks for the post.

  • Awesome Thanks for putting that up, and thanks again for your help!!

  • Hi! I had an issue copying files that I could solved it in this way:

    runakay.blogspot.com/.../problem-copying-large-path-files.html

  • Went to the download site for XP sp3 version to get PS2 and it kept downloading version 1???

  • blogs.technet.com/.../install-powershell-on-windows-xp-and-copying-files.aspx

  • Powershell is still 1.0. It is 1.0 V2.

    At  Poswershell prompt type: $PSVersionTable which will give you all of the version information.

    V1 is no longer available for download.

  • @gcdelie@msn.com Windows PowerShell 2.0 installs in a folder ...windows\system32\windowspowershell\v1.0 this is for backwards compatability, and has nothing to do with the ACTUAL version of Windows PowerShell being installed. The link above IS for Windows PowerShell 2.0. as @JRV mentioned, there is a $PSVersionTable automatic variable you can use to see the version of Windows PowerShell. This variable did not exist on Windows PowerShell 1.0, but it will tell you PSVersion 2.0 if you have Windows PowerShell 2.0 installed. This variable is very useful, because it also reports other information such as CLRVersion, WSManStackVersion, OS BuildVersion and other real cool information.

  • Hi Scripting Guy

    I need to use powershell to run on my AD to findout the user's account that has been deleted for teh past 90 days.

    Thank You

  • thankalot.. i searched deeply .. finally i got solution from this blog , thank you very much

  • If you want to install mvc 4 with vs2010 sp1 then this article(1st) is important and also the below link.....

    support.microsoft.com/.../968929

  • I have Win XP Professional on my Dell computer. Somehow the boot.ini has been deleted or corrupted. My computer won't boot into Windows. I have the Product Key - but not the original Windows XP Prod. disc. How do it go about using the Prod. Key to open the Windows so I can correct the boot.ini issue?
    Thank you for any help you can give me!
    MW