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
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.