0

I want to create a PowerShell script to copy the Templates folder from a remote computer to a local computer:

# Ask the administrator to enter the name of the remote PC
$RemotePCName = Read-Host "Please enter the name of the remote PC"

# Ask the administrator to enter their credentials
$Credential = Get-Credential -Message "Enter your administrator credentials to connect to remote PC $RemotePCName"

$IdenUser = Read-Host "Please enter user account ID"

#Templates folder path:
$path_outlook_templates = "C:\Users\$IdenUser\AppData\Roaming\Microsoft\Templates"
# Local PC variable declaration:
$local_pc_path = "C:\Users\ba89260\Documents\test"

$LocalPCName = $env:COMPUTERNAME

# Creating a session between the remote PC and the local PC
Write-Host "Creating a session between the remote PC $RemotePCName and the local PC $LocalPCName:"
$Session = New-PSSession -ComputerName $RemotePCName -Credential $Credential
if ($Session -eq $null) {
    [System.Windows.Forms.MessageBox]::Show("The connection between computer $RemotePCName and computer $LocalPCName could not be established.", "Session error", "OK", "Error" )
    exit
}
Write-Host "Success! Next step"

# Copy Outlook Templates folder
Write-Host "Copying the Templates folder..."
Copy-Item -Path $outlook_templates_path -Destination $local_pc_path -FromSession $Session -Recurse -Force
Write-Host "Success! Next step"#

# Displaying a Windows window
#[Opening Windows message box]::Show("Message to display in the window", "Title in the window", "button", "icon in the message")
[System.Windows.Forms.MessageBox]::Show("Copying data from $RemotePCName to $LocalPCName is complete.", "Done", "OK", "Information")

The script does what I ask of it EXCEPT that the console returns me the 'Stream' error in a loop :

Copy-Item: Could not find a parameter matching the name "Stream".
To the character Line 31: 1
+ Copy-Item -Path $outlook_templates_path -Destination $pc_path ...
+ ~~~~~~~~~~~~~
    + CategoryInfo: InvalidArgument: (:) [Get-Item], ParameterBindingException
    + FullyQualifiedErrorId: NamedParameterNotFound,Microsoft.PowerShell.Commands.GetItemCommand

I tested with -LiteralPath or -Path but the result is the same. And I want to point out that I was able to copy this folder from the local computer to the remote computer...and I didn't get any errors. Can you please help me find a solution? Thanks for your help

2
  • 1
    Early in the script the variable $path_outlook_templates is created. But, Copy-Item uses $outlook_templates_path. Commented May 9, 2024 at 23:09
  • Thank you for your reply. Yes there is an error in the script but it is because of the French English translation that there is a variable error (to make this post). Otherwise, there is no this error in my program and my "Stream" error does not come from this variable Commented May 12, 2024 at 15:13

1 Answer 1

0

The error you're encountering is because the Copy-Item cmdlet is expecting a parameter named -Stream, which doesn't exist. The -Stream parameter is not a valid parameter for the Copy-Item cmdlet.

Correct code:

# Copy Outlook Templates folder
Write-Host "Copying the Templates folder..."
Get-ChildItem -Path $outlook_templates_path -Recurse | Copy-Item -Destination $local_pc_path -Force
Write-Host "Success! Next step"

this code, is using the Get-ChildItem cmdlet to get the files and subfolders in the $outlook_templates_path directory, and then piping the result to the Copy-Item cmdlet. This way, it´s copying all files and subfolders in the remote directory to the local directory.

Also, note that the -FromSession parameter is not needed in this case, as it´s not copying files from the local machine to the remote machine. Instead, it´s copying files from the remote machine to the local machine.

Sign up to request clarification or add additional context in comments.

1 Comment

Hello, thank you very much for your very comprehensive message. I tested it and the error is indeed gone. This is extraordinary !!! Just a clarification: if I put -recurse after $outlook_templates_path, it copies the elements in bulk to the destination folder... I removed it (moved after $local_pc_path) and the copy respects the folders inside. On the other hand, my question: how to copy empty folders? After reading the Microsoft documentation, it is not possible to copy empty folders. Thank you again for solving my problem and if you have an answer for empty folders, don't hesitate Alexis

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.