How to Fix: Unable to Copy Files in PowerShell using PSSession and Copy-Item cmdlet without a Method Invocation Error
Learn how to fix: Unable to Copy Files in PowerShell using PSSession and Copy-Item cmdlet without a Method Invocation Error.
📋 Table of Contents
The error 'Method invocation failed because [System.IO.MemoryStream] does not contain a method named ''new''.' occurs when attempting to copy files from an old machine to a new one using PSSession and Copy-Item cmdlet in PowerShell v5 on Windows 10. This issue affects users who rely on PowerShell scripts for file transfer, causing frustration during the process.
This error is particularly frustrating as it prevents the successful completion of file transfers, which can lead to data loss or corruption. In this troubleshooting guide, we will explore the root causes of this error and provide two primary fix methods to resolve the issue.
💡 Why You Are Getting This Error
- The primary reason for this error is that certain files downloaded from the internet may be blocked by Windows Defender SmartScreen, which prevents them from being copied using PSSession. This occurs because these files are not digitally signed or have been flagged as malicious.
- An alternative reason for this error could be due to the use of a non-standard character set or encoding in the file names, which can cause issues with the Copy-Item cmdlet.
🔧 Proven Troubleshooting Steps
Unblocking files using Windows Defender SmartScreen
- Step 1: Open the Command Prompt as an administrator on the new machine and navigate to the directory containing the blocked files. Type the following command: `powershell -Command "Get-Process -Name windowsdefendermainservice | Stop-Process -Force"` to disable Windows Defender SmartScreen temporarily.
- Step 2: Next, use the Copy-Item cmdlet with the `-Recurse` and `-FromSession` parameters to copy the blocked files. The command should look like this: `Copy-Item -Path "C:ileslockedList.txt" -Destination 'C:\files' -Recurse -FromSession $PSSession -PassThru -Force`.
- Step 3: After copying the blocked files, enable Windows Defender SmartScreen by running the following command: `powershell -Command "Get-Process -Name windowsdefendermainservice | Start-Process -Action restart"`.
Using a different character set or encoding
- Step 1: Check if the file names contain non-standard characters or special characters that may be causing issues with the Copy-Item cmdlet. Use the following command to view the character encoding of a file: `Get-FileInformation -Path "C:ilesile.txt" | Select-Object -ExpandProperty Encoding`.
- Step 2: If the file names are using a different character set or encoding, try renaming them to use only standard characters and special characters. You can use the `Rename-Item` cmdlet with the `-NewName` parameter to rename files: `Rename-Item -Path "C:ilesile.txt" -NewName 'C:\files\newfile.txt'`.
- Step 3: Alternatively, you can use the `Copy-Item` cmdlet with the `-Encoding` parameter to specify the character encoding of the file. For example: `Copy-Item -Path "C:ilesile.txt" -Destination 'C:\files' -Recurse -FromSession $PSSession -PassThru -Force -Encoding UTF8`.
- Step 4: After making these changes, try copying the files again using PSSession and Copy-Item cmdlet.
✨ Wrapping Up
To resolve the issue of 'Method invocation failed because [System.IO.MemoryStream] does not contain a method named ''new''.' when transferring files from an old machine to a new one using PSSession and Copy-Item cmdlet in PowerShell v5 on Windows 10, you can try two primary fix methods: unblocking files using Windows Defender SmartScreen or using a different character set or encoding. By following the steps outlined in this guide, you should be able to successfully copy files from the old machine to the new one without encountering this error.
❓ Frequently Asked Questions
🛠️ Related Fixes
How to Fix: Pc crashes shortly after launching game (rainbow
Fix Pc crashes shortly after launching game (rainbow six siege). Compl
How to Fix: Installing an APK on a locked down phone
Installing an APK on a locked down phone: Try using a rooted device, e
How to Fix: New PC build- no signal and no clue
Fix New PC build- no signal and no clue. Complete troubleshooting guid