Powershell Copy-Item -recurse does not pick up new directory - powershell

I'm creating a binary PS module with PlatyPS help. I have a local poor-man's deploy script like this (PS 5.1):
$modulepath= "$Env:USERPROFILE\Documents\WindowsPowerShell\Modules"
$releasePath = ".\bin\release\net472"
# build project
dotnet build -c release
# build documentation
# requires PlatyPS module
New-ExternalHelp -Path .\docs -OutputPath $releasePath\en-US -Force
ls $releasePath # debug
# copy files
Get-ChildItem -Path $releasePath | Copy-Item -Destination $modulepath\PoshCommence -Recurse -Force
ls $modulepath\PoshCommence # debug
This ouputs the following surprising result:
Directory: X:\CustomModules\PoshCommence\bin\release\net472
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 17-2-2021 00:31 en-US
-a---- 17-2-2021 00:24 36352 PoshCommence.dll
Directory: C:\Users\XXX\Documents\WindowsPowerShell\Modules\PoshCommence
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 17-2-2021 00:31 871 about_PoshCommence.help.txt <- from en-US folder
-a---- 17-2-2021 00:24 36352 PoshCommence.dll
-a---- 17-2-2021 00:31 141080 PoshCommence.dll-Help.xml <- from en-US folder
None of the directories exist prior to running the script. I deleted the 'bin' project folder as well as the 'PoshCommence' module folder.
It seems either Get-ChildItem or Copy-Item -Recurse do not pick up the newly created 'en-US' directory, but the contents of it do get copied to the root level. If I run the script a second time (without deleting folders), it works as expected (except I still have docs stuff in the root of the module I don't want).
That has me stumped. I have tried -Verbose on everything, I put Start-Sleep after every line thinking operations may need time, but to no avail. Why isn't the 'en-US' folder picked up the first time?

Answering my own question. My confusion stems from the fact that
Copy-Item -Path $source -Destination $destination -Recurse
Works differently depending on whether the folder in $destination already exists or not.
So in the end all it took was
if (!(Test-Path $modulepath\PoshCommence)) { mkdir $modulepath\PoshCommence }
prior to the copying.

Related

Find all files with the exact extension in Powershell

I'm trying to get all the files that end with ".asp" but I also get the files that ends in ".aspx" or ".asp..." when I do :
Get-ChildItem C:\test -Filter *.asp -Recurse | % {$_.FullName}
For the example, let's say I have a directory with test1.asp, test2.aspx and test3.asp, if I execute my script, the output will be:
C:\test\test1.asp
C:\test\test2.aspx
C:\test\test3.asp
but I only wanted it to get me "test1.asp and test3.asp".
For information, I use Powershell 2.1.
Can someone tell me how to fix that?
Try to check one more the last 3 symbols
Get-ChildItem 'C:\test' -Filter '*.asp' -Recurse |
Where {$_.Name.substring($_.Name.length -3, 3) -Match 'asp'} | % {$_.FullName}
Caveat: PowerShell behavior seems to have changed!
Windows PowerShell 5.1.19041.2364:
PS C:\Users\OA> Get-ChildItem -File -Path "C:\Test" -Filter "*.txt"
Verzeichnis: C:\Test
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 05.02.2023 09:59 0 TestFile.txt
-a---- 05.02.2023 09:59 0 TestFile.txtLirumLarumLöffelstiel
PowerShell Core 7.3.2:
PS C:\Users\OA> Get-ChildItem -File -Path "C:\Test" -Filter "*.txt"
Directory: C:\Test
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 05.02.2023 09:59 0 TestFile.txt
Both commands were performed on Microsoft Windows Pro 10.0.19045.2546 (22H2).
Behavior on legacy operating systems: PS version 2.0 on Windows XP (using wildcard path instead of -File parameter) and PS version 5.14409.1018 on Windows 7: both have the former behavior.

Is there a specific command in Powershell for moving a file?

I've been trying to move a file using Microsoft Powershell,
I've looked on the Microsoft website and didn't understand the instructions.
Here is the directory contents as shown by Get-ChildItem:
Directory: C:\Users\Username\lpthw
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 5/19/2020 6:38 PM useful
-a---- 5/9/2020 2:08 PM 263 drill5.py
Q: I would like to know how I can move the file drill5.py to the directory useful.
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/move-item?view=powershell-7
Move-Item -Path .\drill5.py -Destination .\useful\
# Another way. mv is an alias for Move-Item
mv .\drill5.py .\useful\

Renaming my Item with File Name as a Variable not Working using Powershell

I am using Powershell. My goal is to copy one item to a different directory (which I already know how to do), get the userID (which I already know how to do), and rename the file with a file name that exists in a variable (which I am not sure what I am doing wrong).
When I run my code, it gives me an error: Rename-Item : Cannot bind argument to parameter 'NewName' because it is null. I don't know what I am doing wrong.
I got this from https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/rename-item?view=powershell-7
My code is listed below:
$name = [Environment]::UserName
$mainFile = "\\example\example.xlsx"
$copiedFile = "\\example\example\copiedFolder\example.xlsx"
#Once I have copied the file to the new directory, I do the following code which does not work:
$copiedFile = Rename-Item -Path $copiedFile -NewName $name.xlsx
You've way over complicated this. It's a simple copy command.
Get-ChildItem -Path 'D:\temp\reconcile.*'
<#
# Results
Directory: D:\temp
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 03-Feb-20 12:54 644 reconcile.txt
#>
$mainFile = 'D:\temp\reconcile.txt'
$FileTarget = 'D:\temp\'
Copy-Item -Path $mainFile -Destination "$FileTarget\reconcile.xlsx" -Force
Get-ChildItem -Path 'D:\temp\reconcile.*'
<#
# Results
Directory: D:\temp
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 03-Feb-20 12:54 644 reconcile.txt
-a---- 03-Feb-20 12:54 644 reconcile.xlsx
#>
I am not sure why this...
[Environment]::UserName
...is in your code, and in it twice, since you are not showing how it is being used. However, you can just use the built-in variable for that.
$env:USERNAME
About_EnvironmentVariables
I found the solution, and I feel incredibly dumb for not doing it first. I thank everyone for their input and apologize for all the edits in my previous code. My problem was that I wasn't putting double quotes around $name.xlsx.
This gives me \example\example\copiedFolder\JohnDoe.xlsx Tried single quotes and gave me \example\example\copiedFolder\$name.xlsx instead. Not really sure why. See code below:
$copiedFile = Rename-Item -Path $copiedFile -NewName "$name.xlsx"

Powershell Copy-Item not keeping folder structure

I have the below script to copy data from a local folder to a remote folder created with the current date. However the files are copying but the folder structure is not.
$Date = (Get-Date).ToString("MMddyyyy"),$_.Extension
$Source = "E:\Folder1\\*"
$Dest = "\\Server\Share\Folder2"
$Username = "Username"
$Password = ConvertTo-SecureString "Password" -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential($Username, $Password)
Remove-PSDrive -Name T
Start-Sleep -s 1
New-PSDrive -Name T -PSProvider FileSystem -Root $Dest -Credential $mycreds -Persist
if (!(Test-Path "T:\$Date"))
{
md -Path "T:\$Date"
}
Get-ChildItem -Path $Source -Recurse | % { Copy-Item -Path $_ -Destination "T:\$Date" -Container -Force -Verbose }
Could anyone advise where I am going wrong here?
Thank you.
Nice script, I think we can get this sorted in no time!
Why this happened
The reason this is failing is in this step right here:
Get-ChildItem -Path $Source -Recurse
The -Recurse switch is causing you pain. To illustrate why this is, I created a simple folder structure.
When you run Get-ChildItem -Path $Source -Recurse alone, you'll get a recursive listing of all files in the $Source path, like so:
PS C:\temp\stack> Get-ChildItem -Recurse
Directory: C:\temp\stack
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 8/4/2017 10:50 AM Source
Directory: C:\temp\stack\Source
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 8/4/2017 10:57 AM 1
d----- 8/4/2017 10:57 AM 2
d----- 8/4/2017 10:57 AM 3
d----- 8/4/2017 10:57 AM 4
Directory: C:\temp\stack\Source\1
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 8/4/2017 10:57 AM 20 Archive.rar
-a---- 8/4/2017 10:56 AM 0 File01.bmp
Well, in the next step of your script, you pipe that output over to Copy-Item for every single file.
You're basically expressly telling PowerShell 'take this folder and all of it's subfolders and everything, and dump them all in this one folder, ignoring the folder structure'
How to fix
What you really want to do is simply move the -Recurse parameter over to Copy-Item, and you're done :)
Get-ChildItem -Path $Source |
Copy-Item -Destination "T:\$Date" -Container -Recurse -Force -Verbose
Hope that helps, have a nice 🎃day!
This question is a few years old, but in case someone arrives here like I did...
In another post, a user suggested using robocopy. It worked great for me:
robocopy "source/folder" "target/folder" "*.file_extension" /s
This command copies from the source folder the files that match the given filter, replicating the folder structure in the destination folder.
In this case, it would be used like:
robocopy "E:\Folder1\" "\\Server\Share\Folder2" "*" /s

Wildcard matching in Get-ChildItem in PowerShell

Is there a way to determine how wildcard matching is done in Get-ChildItem?
Various articles (1, 2) suggest that it is done through the WildcardPattern class, but I don’t think this is the case. For example, suppose you have a file in C:\test\test2\testfile.txt. Then Get-ChildItem –Path “C:\*\testfile.txt” will not find the file while WildcardPattern::IsMatch will. Wildcard "*" matching in Get-ChildItem seems to be on directory level: so "\*\" will never match more than one level, like "\A\B\".
So if WildcardPattern class isn't used, then what is?
From what I know, it's using the WildcardPattern as you describe. However, the cmdlet Get-ChildItem limits it to the current directory (characters except \), so it won't conflict with the -Recurse switch that goes to unlimited levels.
With "C:\*\testfile.txt", the asterisk plays a role just for the first level directory (e.g test). The file you're looking for is not there and the output you get is expected. Add another asterisk for the second level and you'll get the desired output (e.g "C:\*\*\testfile.txt"). You can also add the Recurse switch to start searching from the current location, all the way downwards.
Either would work:
gci c:\test\*\testfile.txt
or
gci c:\*\testfile.txt -recurse
Example:
PS C:\temp\test2> dir
Directory: C:\temp\test2
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 4/4/2013 10:41 PM 0 testfile.txt
PS C:\temp\test2> cd \
PS C:\> gci c:\*\testfile.txt -recurse -ea SilentlyContinue
Directory: C:\Temp\test2
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 4/4/2013 10:41 PM 0 testfile.txt