I have a long list of find and move operations, I want to slim down the script by substituting the following 2 long commands with 2 short variables.
Short variables:
$f = 'Get-ChildItem -Recurse -Filter'
$m = 'Move-Item -Force -Verbose -Destination V:\MSL\_pdf\'
Long commands:
Get-ChildItem -Recurse -Filter GAS*.pdf | Move-Item -Force -Verbose -Destination V:\MSL\_pdf\GAS
Get-ChildItem -Recurse -Filter GCA_00*.pdf | Move-Item -Force -Verbose -Destination V:\MSL\_pdf\GCA\GCA_00
Get-ChildItem -Recurse -Filter GCA_01*.pdf | Move-Item -Force -Verbose -Destination V:\MSL\_pdf\GCA\GCA_01
And this doesn't work:
$f GAS*.pdf | $m`GAS
$f GCA_00*.pdf | $m`GCA\GCA_00
$f GCA_01*.pdf | $m`GCA\GCA_01
As #Lee said in the comment, you should use function:
function GetAndMove($files, $dest)
{
$dest = "V:\MSL\_pdf\$($dest)"
Get-ChildItem -Recurse -Filter $files | Move-Item -Force -Verbose -Destination $dest
}
# Now call the function
GetAndMove "GAS*.pdf" "GAS"
GetAndMove "GCA_00*" "GCA\GCA_00"
GetAndMove "GCA_01*" "GCA\GCA_01"
...
Related
I tried
Get-ChildItem -Path 'C:\temp' -Recurse Select Name | Where {$_ -notlike 'C:\temp\one*'} | sort length -Descending | Remove-Item -force
but it doesn't work
Get-ChildItem : A positional parameter cannot be found that accepts argument 'Name'
What's wrong
You were missing a |
Get-ChildItem -Path 'C:\temp' -Recurse | Select -ExpandProperty FullName | Where {$_ -notlike 'C:\temp\one*'} | Remove-Item -force
Try this with -Exclude (And why sort when deleting files?)
Get-ChildItem -Path 'C:\temp' -Recurse -Exclude 'C:\temp\one*' | Remove-Item -force
Use the function below:
Function Delete-Except
{
$path = ""
$exceptions = #(
#Enter files/folders to omit#
)
try:
Get-ChildItem $source -Exclude $exceptions| Remove-Item $_ -Force -Recurse
catch:
Write-Host "Delete operation failed." - Foregroundcolor Red
Pause
}
I got a script:
$CopySource = "C:\Source\Qlikview Storage\PrivateData\Gemensamma\Qvd_Raw\Agresso"
$CopyDestination = "C:\Dest\Qlikview Storage\PrivateData\Gemensamma\Qvd_Raw\Agresso"
$files = Get-ChildItem -File "*.qvd" $CopySource -Force
foreach ($file in $files) {
Copy-Item -path $CopySource\$_$file -Destination $CopyDestination -Force
}
$CopySource = "C:\Source\Qlikview Storage\PrivateData\Gemensamma\Qvd_Raw\Agresso"
$CopyDestination = "C:\Dest\Qlikview Storage\PrivateData\Gemensamma\Qvd_Raw\Agresso"
$items = Get-ChildItem -File "*.qvd" $CopyDestination
foreach($I in $Items) {
$newfilename= rename-item -path $I.Name -newname ("Agresso_" + $I.Name)
The problem I have is that I need to copy files from source to destination and once they are over they need to have agresso_ added. Then they day after a batch of files will be copied again and also they need to be renamed to agresso_ and overwrite the old ones, preferably with move-item. I got a problem already, as I am not used with prefixes,
I tried enless of versions with where-object and simular, could not figure out a way to use test-path either.
I did exactly this but with renaming the files, like this for maximo:
$files = Get-ChildItem -File "*.qvd" $CopySource -Force
foreach ($file in $files) {
Copy-Item -path $CopySource\$_$file -Destination $CopyDestination -Force -Verbose 4>&1 |
Out-File -Append $logpath
}
"Klar med Kopiering av .qvd filer $global:currenttime" | Out-File $logpath -Append
"Påbörjar omdöpning av .qvd filer $global:currenttime" | Out-File $logpath -Append
$items = Get-ChildItem -File "*.qvd" $CopyDestination
foreach($I in $Items) {
$newfilename=$I.Name.Replace("QVDLager1","Maximo")
Move-Item -Path $I.FullName -Destination $CopyDestination\$newfilename -Force -Verbose 4>&1 |
Out-File -Append $logpath
If anyone can help me in the right direction it would be highly appriciated.
You can copy (or move) and rename the destination at the same time:
$CopySource = "C:\Source\Qlikview Storage\PrivateData\Gemensamma\Qvd_Raw\Agresso"
$CopyDestination = "C:\Dest\Qlikview Storage\PrivateData\Gemensamma\Qvd_Raw\Agresso"
Get-ChildItem -Path $CopySource -File "*.qvd" -Force | ForEach-Object {
# create the full path for the target file with "Agresso_" prefixed
$target = Join-Path -Path $CopyDestination -ChildPath ('Agresso_{0}' -f $_.Name)
$_ | Copy-Item -Destination $target -WhatIf
}
If satisfied with the results of the code shown in the console, you can remove the -WhatIf switch to really start copying (or moving) the files.
I'm trying to delete "only" specific filetypes in a folder and all sub-folders, but keeping all sub-folders intact.
i'm a bit of a novice, but I figured both of these were correct, but they spit out errors instead:
get-childitem $sourceDir -exclude .dll,.lib -recurse | remove-item
Fails with this error: remove-item : Windows PowerShell is in NonInteractive mode. Read and Prompt
This one:
#Remove-Item $sourceDir -recurse -exclude .dll,.lib | Where { ! $_.PSIsContainer }
Simply does nothing.
Try this for PS V2:
$excluded = #("*.dll", "*.lib")
get-childitem -path $sourceDir -exclude $excluded -Recurse | where { ! $_.PSIsContainer } | Remove-Item -Force
Or this for V3
$excluded = #("*.dll", "*.lib")
get-childitem -path $sourceDir -File -exclude $excluded -Recurse | Remove-Item -Force
I want to copy a folder to a range of computers on my LAN. Here is what I have so far:
$Computers = "Get-Content C:\Scripts\computers.txt"
$Source = "C:\Install\9_10_00_08HotFix_201504140001"
$Destination = "\\192.168.6.$\c$\Install"
ForEach-Object {
Copy-Item -Path $Source -Recurse -Destination $Destination -Verbose -Force -ErrorAction SilentlyContinue
}
You didn't say what your actual problem is, but it's probably that you misplaced the first double quote in the Get-Content statement, that you can't define a variable destination like you do, and that you don't do anything with what you (try to) read from computers.txt.
Change this:
$Computers = "Get-Content C:\Scripts\computers.txt"
$Source = "C:\Install\9_10_00_08HotFix_201504140001"
$Destination = "\\192.168.6.$\c$\Install"
ForEach-Object {
Copy-Item -Path $Source -Recurse -Destination $Destination -Verbose -Force -ErrorAction SilentlyContinue
}
into this:
$Computers = Get-Content "C:\Scripts\computers.txt"
$Source = "C:\Install\9_10_00_08HotFix_201504140001"
$Computers | ForEach-Object {
Copy-Item -Path $Source -Recurse -Destination "\\192.168.6.$_\c$\Install" -Verbose -Force -ErrorAction SilentlyContinue
}
and your code should work.
Why does
gci $from -Recurse | copy-item -Destination $to -Recurse -Force -Container
not behave in the same way as
copy-item $from $to -Recurse -Force
?
I think it should be the same, but somehow it's not. Why?
You are not looping over each item in the collection of files/folders, but passing the last value to the pipe. You need to use Foreach-item or % to the Copy-Item command. From there, you also do not need the second -recurse switch as you already have every item in the GCI.
try this:
gci $from -Recurse | % {copy-item -Path $_ -Destination $to -Force -Container }
Here is what worked for me
Get-ChildItem -Path .\ -Recurse -Include *.txt | %{Join-Path -Path $_.Directory -ChildPath $_.Name } | Copy-Item -Destination $to
This works for me:
Get-ChildItem 'c:\source\' -Recurse | % {Copy-Item -Path $_.FullName -Destination 'd:\Dest\' -Force -Container }
To loop all items, this should work:
gci -Path $from -Recurse | % { $_ | copy-item -Destination $to -Force -Container}
Just do the foreach and pipe each item again.
The switches are wrong. They should be:
gci -Path $from -Recurse | copy-item -Destination $to -Force -Container