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\
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 months ago.
Improve this question
I am wanting to send out a file that others in my company can use to update a local folder from a Google Drive folder.
I have a PowerShell 7 script that works on my local computer:
robocopy "C:\Users\(insert username)\AppData\Roaming\Dynamo\Dynamo Revit\2.13\packages" "G:\Shared drives\(Insert Drive Name)\STANDARDS\DYNAMO\Packages\2.13\packages" /mir
I tried making a batch file using %appdata% to send out but it would just create a new folder in the folder containing the script. I then read that powershell doesn't use %appdata%
I tried using "$env:appdata" to locate the local file but nothing would happen. Can anyone help me with the proper syntax?
Here is the script I was trying:
robocopy "G:\Shared drives\(insert drive name)\STANDARDS\DYNAMO\Packages\2.13\packages" "$env:appdata\..\Roaming\Dynamo\Dynamo Revit\2.13\packages" /mir
Thanks
This ...
robocopy "G:\Shared drives\(insert drive name)\STANDARDS\DYNAMO\Packages\2.13\packages" "$env:appdata\..\Roaming\Dynamo\Dynamo Revit\2.13\packages" /mir
... is not a valid path.
$env:appdata evaluates to the following:
(Get-ChildItem -Directory -Path $env:APPDATA).Parent
# Results
<#
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 9/7/2022 9:18 AM Roaming
d----- 9/7/2022 9:18 AM Roaming
#>
Get-ChildItem -Directory -Path $env:APPDATA
# Results
<#
Directory: C:\Users\WDAGUtilityAccount\AppData\Roaming
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 9/7/2022 9:18 AM Adobe
d---s- 9/21/2022 6:50 PM Microsoft
#>
Get-ChildItem -Directory -Path "$env:APPDATA\..\Credentials"
# Results
<#
Get-ChildItem : Cannot find path 'C:\Users\WDAGUtilityAccount\AppData\Credentials' because it does not exist.
At line:1 char:1
#>
Get-ChildItem -Directory -Path "$env:APPDATA\*\Credentials"
# Results
<#
Mode LastWriteTime Length Name
---- ------------- ------ ----
d---s- 9/7/2022 9:18 AM Credentials
#>
I have a folder in windows that's name is RESIDENCES INC.. The problem is this is actually an invalid foldername because of the period in it. If I try to delete it, it says
Could not find this item. This is no longer located in <path to folder>. Verify the item's location and try again.
How can I remove it with code?
That's weird, might be a Windows thing? It worked fine for me:
PS /home/Documents/test> mkdir "RESIDENCES INC."
PS /home/Documents/test> gi './RESIDENCES INC./'
Directory: /home/Documents/test
Mode LastWriteTime Length Name
---- ------------- ------ ----
d---- 3/18/2021 12:04 AM RESIDENCES INC.
PS /home/Documents/test> (gi './RESIDENCES INC./').FullName
/home/Documents/test/RESIDENCES INC./
PS /home/Documents/test> Remove-Item (gi './RESIDENCES INC./').FullName -Force
PS /home/Documents/test> (gi './RESIDENCES INC./').FullName
Get-Item: Cannot find path '/home/Documents/test/RESIDENCES INC./' because it does not exist.
PS /home/Documents/test>
Can you paste the command you're using to delete the folder?
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.
I can't recursively remove the pyc files from my Python project.
Here is the project:
Directory: C:\Users\jk025523\projects\ex47
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 10/07/2016 01:52 bin
d----- 10/07/2016 01:52 docs
d----- 10/07/2016 01:52 ex47
d----- 10/07/2016 01:52 tests
-a---- 09/07/2016 21:02 521 setup.py
There are indeed some pyc files inside the tests directory:
Directory: C:\Users\jk025523\projects\ex47\tests
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 09/07/2016 21:28 180 ex47_tests.py
-a---- 09/07/2016 21:28 721 NAME_tests.pyc
-a---- 05/07/2016 10:52 0 __init__.py
-a---- 05/07/2016 11:37 140 __init__.pyc
However when I enter this command in PowerShell to remove all the pyc files:
find . -name "*.pyc" -exec rm -rf {}
PowerShell outputs this error:
FIND: Parameter format not correct
Anybody know how I can remove all the pyc files from this Python project?
The command you're trying to use is the Linux/Unix find command, which doesn't work on Windows (unless you have something like Cygwin installed, which I don't recommend). Windows has a command with the same name, but different functionality (works more like grep). PowerShell does not have a find cmdlet or alias. You'd do recursive deletion of files with a particular extension like this in PowerShell:
Get-ChildItem -Filter '*.pyc' -Force -Recurse | Remove-Item -Force
If the "find" command you are running is of the linux/unix equivalent, your --exec rm -rf {} will need a delimiter at the end. Like so:
find . -name "*.pyc" -exec rm -rf "{}" \;
I would also recommend always wrapping the variable brackets ({}) in quotes to ensure you are not deleting more than what you are trying to delete.
EDIT: Looks like you are using the Windows find variant, which does not support the arguments you are attempting to use. You can see this page for syntax examples.
You may want to look at one of these answers for alternatives.
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