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.
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 zip file from a directory in two ways in Win10.
The first method using "Right-Click...Send To...Compressed (zipped) folder"
The second method using Powershell Compress-Archive. I wanted to automate the process using Powershell.
The zip file is a library to be imported into the Arduino IDE.
The first method will import into the Arduino IDE correctly. The second method does not import correctly.
Now before you say this is a Arduino problem please continue to read.
So to make sure the files are the same I used Powershell to generated MD5 checksums for both files. To my surprise they were different!!
The only difference I have found are the size of the created zip files. They are created from the same source files.
Why are they different? I can manually unzip both files by "Double-Clicking and Extract All" within File Explorer
Thank you for any advice/help. John.
Here is output of the powershell script...
Directory: C:\file1
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 05/16/2020 17:18 1948 t_OS.zip
Algorithm : MD5
Hash : 19249C2BB9B50E827A77F7A2952EFF6D
Path : C:\file1\t_OS.zip
Directory: C:\file2
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 05/16/2020 19:23 1724 t_OS.zip
Algorithm : MD5
Hash : 2BF7B111F54FC0555E6CA64719AD350F
Path : C:\file2\t_OS.zip
...
#
# sourceFiles are what I need to be zipped. It contains the
# directory t_OS and I need to create t_OS.zip
#
$sourceDir = "C:\sourceFiles"
$sourceFiles = "C:\sourceFiles\t_OS"
# file1 zip was created by right-clicking on the directory t_OS
# in the $sorceDir and selecting Send To, then
# Compressed (zipped) folder. This file was then moved to...
#
$file1 = "C:\file1\t_OS.zip"
# file2 zip is created by powershell using Compress-Archive below.
$file2 = "C:\file2\t_OS.zip"
Get-ChildItem -LiteralPath $sourceDir -Name -Recurse
If (Test-Path $file2) {Remove-Item $file2}
Compress-Archive -DestinationPath $file2 -CompressionLevel Fastest -Path $sourceFiles
Get-ItemProperty -LiteralPath $file1
Get-FileHash -Path $file1 -Algorithm MD5
Get-ItemProperty -LiteralPath $file2
Get-FileHash -Path $file2 -Algorithm MD5
...
The fact that the two zip files are different sizes can have a number of reasons. Here are a few
As already mentioned, the two zip files could be using different compression algorithms. Given the small size of the resultant zip files, it may be that one is storing the data compressed, while the other is storing it uncompressed.
There are a number of extensions that have been added to zip files over the years that have added things like more accurate timestamps. Those add more bytes the the resultant zip files, but they shouldn't prevent the file from being uncompressed. I believe a "right-click" created zip file on Windows 10 doesn't use any of these extensions. Not sure about Powershell.
Do you have access to a Linux setup? There are tools on it that can analyse the structure of zip files.
Also, can you share the Powershell command line you used to create the zip file?
Edit
On your Ubuntu setup, start by checking what unzip thinks about the two files
unzip -t t_OS.zip
If unzip finds an issue it will report it.
If that doesn't work run zipdetails against both zip files and update your question with the results.
zipdetais -v t_OS.zip
If you don't have zipdetails on your Ubuntu setup, get it from here
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\
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"