Recursively copy a set of files from one directory to another in PowerShell

You guys are making this hideously complicated, when it’s really simple: Copy-Item C:\Code\Trunk -Filter *.csproj.user -Destination C:\Code\F2 -Recurse Will copy the Directory, creating a “Trunk” directory in F2. If you want to avoid creating the top-level Trunk folder, you have to stop telling PowerShell to copy it: Get-ChildItem C:\Code\Trunk | Copy-Item -Destination C:\Code\F2 -Recurse -filter … Read more

Copy-item Files in Folders and subfolders in the same directory structure of source server using PowerShell

This can be done just using Copy-Item. No need to use Get-Childitem. I think you are just overthinking it. Copy-Item -Path C:\MyFolder -Destination \\Server\MyFolder -recurse -Force I just tested it and it worked for me. edit: included suggestion from the comments # Add wildcard to source folder to ensure consistent behavior Copy-Item -Path $sourceFolder\* -Destination … Read more

relative path in Import-Module

When you use a relative path, it is based off the currently location (obtained via Get-Location) and not the location of the script. Try this instead: $ScriptDir = Split-Path -parent $MyInvocation.MyCommand.Path Import-Module $ScriptDir\..\MasterScript\Script.ps1 In PowerShell v3, you can use the automatic variable $PSScriptRoot in scripts to simplify this to: # PowerShell v3 or higher #requires … Read more

How do I write to standard error in PowerShell?

Use Write-Error to write to stderr. To redirect stderr to file use: Write-Error “oops” 2> /temp/err.msg or exe_that_writes_to_stderr.exe bogus_arg 2> /temp/err.msg Note that PowerShell writes errors as error records. If you want to avoid the verbose output of the error records, you could write out the error info yourself like so: PS> Write-Error “oops” -ev … Read more