Jump to content

Recommended Posts

Posted

Hello,

 

I am very new to Powershell.

 

I hope you can help with the below bit of code? For some reason, it does not show the two files copied successfully. Instead, it tells me all files did not copy ok. The error message is shown below:

 

copy : Illegal characters in path.At C:\Users\rsaddul1.318\Desktop\log.ps1:20 char:1+ copy $_.fullname $destination -Force -Recurse -errorAction silentlyCo ...+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Copy-Item], ArgumentException + FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Comm ands.CopyItemCommand

 

 

My Script is below:

 

# Moves latest two files

Get-ChildItem 'C:\scripts' -File |Sort-Object -Property CreationTime -Descending |Select-Object -Last 2 |Copy-Item -Destination 'C:\TEST' -Force

 

# Delete all Files in C:\temp older than 365 day(s)$Path = "C:\scripts"$Daysback = "-365"$CurrentDate = Get-Date$DatetoDelete = $CurrentDate.AddDays($Daysback)Get-ChildItem $Path | Where-Object { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item -WhatIF

 

# Creates Log File$logProgress = 'C:\scripts\scriptlog.log'$source = 'C:\scripts\*.txt'$destination = 'C:\TEST\*.txt'get-childitem $source -recurse | foreach {copy $_.fullname $destination -Force -Recurse -errorAction silentlyContinueif($? -eq $false){echo "$($_.fullname) did not copy ok to $destination" | out-file -append $logProgress} else {write-output "$($_.fullname) copied OK to $destination" | out-file -append $logProgress}}

 

Regards

RSaddul

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...