How to delete a folder in windows with PowerShell? - powershell

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?

Related

Sending out batch file email to run a powershell script to update users folder [closed]

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
#>

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

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.

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 Release file

I understand that Powershell does not release a file until the script has exited. In my scripting I am writing to a new file as this:
[System.IO.File]::WriteAllText("E:\Outline\newLocations.xml", $locationsContent)
How can I release this file so I can read later in the script?
The WriteAllText method does not lock the file. Here's an example of creating a new text file, writing to it using that method, then reading from it, then deleting it. You can test this as well by creating a file, writing to it, then PowerShell still open try deleting it from Windows Explorer. Classes will have generally have a Dispose method, if you need to release the instance.
New-Item "C:\temp\newLocations.txt" -ItemType File
Directory: C:\temp
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 1/11/2017 12:16 PM 0 newLocations.txt
[System.IO.File]::WriteAllText("C:\temp\newLocations.txt", "test")
Get-Content "C:\temp\newLocations.txt"
test
Remove-Item "C:\temp\newLocations.txt"
get-item "C:\temp\newLocations.txt"
get-item : Cannot find path 'C:\temp\newLocations.txt' because it does not exist.