Powershell Get-ChildItem - Missing header on subsequent calls to same path - windows

Trying to figure out how to get Powershell to display header details on subsequent requests to the same directory path.
Here is a simplified example of what I am trying to do, note that the second call to Get-ChildItem does not display the header details (presumably because it knows its already been called previously within the same scriptblock):
PS C:\TEMP\foo> $path="c:\temp\foo";Get-ChildItem -Path $path;Write-Output "Delete something and display directory contents again...";del $path\*5*;Get-ChildItem -Path $path
Directory: C:\temp\foo
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 9/21/2016 9:54 PM 16 File1.txt
-a--- 9/21/2016 9:54 PM 16 File2.txt
-a--- 9/21/2016 9:54 PM 16 File3.txt
-a--- 9/21/2016 9:54 PM 16 File4.txt
-a--- 9/21/2016 9:54 PM 16 File5.txt
Delete something and display directory contents again...
-a--- 9/21/2016 9:54 PM 16 File1.txt
-a--- 9/21/2016 9:54 PM 16 File2.txt
-a--- 9/21/2016 9:54 PM 16 File3.txt
-a--- 9/21/2016 9:54 PM 16 File4.txt
This appears to be the default behavior if the same path is referenced more than once. I have found that a second header will be generated whenever a different path is provided in the second Get-ChildItem call, but never when the same path is used more than once.
Any ideas on how to force the second header to display like the first while still keeping both of these calls within the same scriptblock?
Thanks!

$path="c:\temp\data";
Get-ChildItem -Path $path
Write-host "Delete something and display directory contents again..."
del $path\*5* -Recurse
Get-ChildItem -Path $path

Add format-table after get-childitem,it will always display the result in table format with headers
Get-childitem $path | format-table

Related

I still can't delete folders using powershell administrator mode

I still can't delete folders using powershell administrator mode
powershell Delete result
PS C:\Program Files (x86)> ls .\Sangfor\
Directory: C:\Program Files (x86)\Sangfor
Mode LastWriteTime Length Name
---- ------------- ------ ----
d---- 2022/12/23 22:18 SSL
PS C:\Program Files (x86)\Sangfor\SSL\SangforPWEx> ls
Directory: C:\Program Files (x86)\Sangfor\SSL\SangforPWEx
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 2021/3/25 10:18 138056 atl100.dll
-a--- 2022/5/5 22:05 533664 ECAgentProxy.dll
-a--- 2021/3/25 10:18 773968 msvcr100.dll
-a--- 2021/7/18 17:35 1610048 SangforPWAlive.dll
-a--- 2022/5/6 17:45 440232 SangforPWEx.exe
-a--- 2022/5/9 9:37 678312 SangforUDProtectEx_202251723165010.dll
-a--- 2022/5/6 17:44 1397160 SangforUDProtectEx.exe
-a--- 2021/3/25 10:18 2656360 sangforvpnlibcrypto-1_1.dll
-a--- 2021/3/25 10:18 623208 sangforvpnlibssl-1_1.dll
I tried to delete the folder in the GUI, but it showed that the folder was occupied, but I could not close the program in the task manager. I also tried to delete using Windows' linux subsystem, but it showed I/O errors. I scanned the folder using anti-virus software and did not find the problem. I hope I can find a way to delete the folder.

Zip individual files in powershell version 3

we have windows 2012 servers and we have powershell version 3. I'm trying to zip log files individually in a folder based on date condition.
Compress-Archive is not available in version 3.
we do not have any 3rd party zip utilities like WinZip, 7z..etc
I tried using [System.IO.Compression.ZipFile] class but it has no flexibility to zip individual files.
My requirement is, to capture the log files based on date and send them through loop to zip each file and delete original file. Your help in this regard is much appreciated.
Thanks in advance.
This answer works fine - it's using COM rather than pure .NET methods:
https://serverfault.com/a/456496/204875
Although, to be honest, I highly recommend downloading the portable version of 7zip and using that. No installation required, a lot more efficient and faster. If you can distribute a script, you can usually distribute an exe with it (although, yes, some environments are highly regulated).
Example:
$srcdir = "C:\tmp\in"
$zipFilepath = "C:\tmp\out"
$files = Get-ChildItem -Path $srcdir | where{! $_.PSIsContainer}
foreach($file in $files) {
$zipFilename = $file.name -replace '\.\w+$','.zip'
$zipFile = "$zipFilepath\$zipFilename"
#Prepare zip file
if(-not (test-path($zipFile))) {
set-content $zipFile ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))
(dir $zipFile).IsReadOnly = $false
}
else {
# exit loop if zipfile with same name exists
write-warning "$zipfile exists"
Continue
}
# create new COM object
$shellApplication = new-object -com shell.application
$zipPackage = $shellApplication.NameSpace($zipFile)
$zipPackage.CopyHere($file.FullName)
#using this method, sometimes files can be 'skipped'
#this 'while' loop checks each file is added before moving to the next
while($zipPackage.Items().Item($file.name) -eq $null){
Start-sleep -milliseconds 250
}
}
write-host "zipfiles created"
Result:
> dir -Recurse
Directory: C:\tmp\in
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 30/12/2021 4:12 pm 5157 txt1.txt
-a---- 30/12/2021 4:12 pm 5157 txt2.txt
-a---- 30/12/2021 4:12 pm 5157 txt3.txt
-a---- 30/12/2021 4:12 pm 5157 txt4.txt
Directory: C:\tmp\out
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 30/12/2021 4:13 pm 1997 txt1.zip
-a---- 30/12/2021 4:14 pm 1997 txt2.zip
-a---- 30/12/2021 4:14 pm 1997 txt3.zip
-a---- 30/12/2021 4:14 pm 1997 txt4.zip
This solution has been thoroughly tested on Windows Server 2012 and doesn't require the shell COM object. Consequently it can run unattended. I have scheduled it to run daily via Windows Task Scheduler.

How to make git print paths with back slashes instead of forward slashes in a Windows console

Is it possible to make git print back slashes instead of forward slashes when it prints out paths in a Windows console? I'd like to be able to copy the paths that it outputs and paste it into my future console commands.
Example output:
C:\vna>git status
On branch herpderp
Your branch is up-to-date with 'origin/herpyderp'.
Untracked files:
(use "git add <file>..." to include in what will be committed)
Java/HerpDerp/src
Java/HerpDerp/src/main/java/com/derp/messaging/
Java/HerpDerp/src/main/java/com/derp/controller/event/
Java/HerpDerp/src/main/java/com/derp/controller/domain/
Java/HerpDerp/src/main/java/com/derp/controller/monitor/
nothing added to commit but untracked files present (use "git add" to track)
Instead of
Java/HerpDerp/src/main/java/com/derp/messaging/
I'd like to see
Java\HerpDerp\src\main\java\com\derp\messaging\
or even
Java\\HerpDerp\\src\\main\\java\\com\\derp\\messaging\\
Edit: I should have clarified that this was for working with git in a Windows console. Also, it seems the correct answer for my case is to use Git Bash.
I know it's a little old, but you can use environment variable substitution to replace the / with \ or \\:
C:\> SET FILE=Java/HerpDerp/src
C:\> ECHO %FILE:/=\%
Java\HerpDerp\src
C:\> ECHO %FILE:/=\\%
Java\\HerpDerp\\src
C:\>
To take it a step further, you can write a batch file that does it for a command that outputs a bunch of paths: (git diff --name-only HEAD~1 in the following example)
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /F "usebackq delims=" %%i IN (`git diff --name-only HEAD~1`) DO (
SET FILE=%%i
ECHO !FILE:/=\!
)
ENDLOCAL
git status will be execute through a git bash session, so it would not ever use \ in path (as seen in "Git Bash for Windows showing/expecting file paths with forward-slashes, no drive colon").
See also "Why Windows Uses Backslashes and Everything Else Uses Forward Slashes"
As mentioned, you would have to post-process the output of the command in order to get the correct path format, as in "Bash converting path names for sed so they escape".
st=$(git status)
echo "${st////\\}"
or
echo "${st////\\\\}"
with:
// / / \\\\}"
^ ^ ^ ^
| | | |
| | | replacement, backslash needs to be backslashed
| | delimiter
| string
global
substitution
Thank you Lance Clark and VonC. I've been using Git with Windows, Unix/Linux, and Mac for several years, but I never understood how Git works through the Windows Command Shell. I came across this question because I was having trouble with forward slashes in the DOS command shell (the Windows Command Prompt).
In the DOS command shell, I was calling the delete command ("del") to delete a file using the file path returned by "git status", which of course had forward slashes in it. As a result, the error "Invalid switch" was returned or if I wrapped the path in quotes the error was "The system cannot find the path specified." That is when I did a web search and came across this stack overflow question.
The answer led me to learn about Git Bash and how Git really works in Windows. As a result, I'm switching to using Git Bash with Windows whenever possible rather than the DOS command shell. That is also making life easier going back and forth between Windows, Linux, and Mac. My forward slash and backward slash nightmares are mostly better now and I can sleep at night. :)
Here are some links to concise information about Git Bash that helped me understand how it works:
Atlassian: Git Bash
superuser: What is Git Bash for Windows anyway?
This is a little old, but I want to add this for people who find it via Google as I did (I was looking for something else).
#aspoon mentioned that you can change \ to / fairly easily in PowerShell. However, the OP mentioned that their goal was to use the output in subsequent commands. That makes this a bit of an XY question: the OP asked how to get git to output Windows-style paths, but then said that their actual goal was this:
I'd like to be able to copy the paths that it outputs and paste them into my future console commands.
If that's the case, another solution is that if you can use PowerShell as your console, or at least make your "future console commands" be PowerShell commands, then your commands will work.
One of the improvements that PowerShell has over previous Windows shells is that it accepts either folder delimiter as input. Note that in the following Get-ChildItem commands, the input paths can use either / or \ as folder delimiters.
This is not meant as a correction to other answers but merely an additional option that I want people to be aware of.
PS> dir \Users\Public\Desktop\ -Force
Directory: C:\Users\Public\Desktop
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 7/3/2022 11:17 PM 2043 CloudBerry Backup.lnk
-a--- 7/29/2022 5:04 PM 1422 EditPad Pro 8.lnk
-a--- 8/31/2022 11:11 AM 993 Firefox.lnk
-a--- 6/17/2022 8:56 AM 1070 GET Help Live Chat.lnk
-a--- 6/17/2022 8:56 AM 1986 GET Help Phone Support.lnk
-a--- 9/16/2022 8:38 PM 2260 Google Chrome.lnk
-a--- 9/27/2022 4:32 PM 2276 Microsoft Edge.lnk
-a--- 7/28/2022 12:47 PM 1363 PowerGREP 5.lnk
-a--- 7/1/2022 4:41 PM 1453 RegexBuddy 4.lnk
-a--- 6/17/2022 1:32 PM 877 TeamViewer.lnk
-a--- 6/30/2022 6:30 PM 993 Visual Studio Code.lnk
-a--- 9/14/2022 9:20 AM 1917 Zoom.lnk
PS> dir /Users/Public/Desktop/ -Force
Directory: C:\Users\Public\Desktop
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 7/3/2022 11:17 PM 2043 CloudBerry Backup.lnk
-a--- 7/29/2022 5:04 PM 1422 EditPad Pro 8.lnk
-a--- 8/31/2022 11:11 AM 993 Firefox.lnk
-a--- 6/17/2022 8:56 AM 1070 GET Help Live Chat.lnk
-a--- 6/17/2022 8:56 AM 1986 GET Help Phone Support.lnk
-a--- 9/16/2022 8:38 PM 2260 Google Chrome.lnk
-a--- 9/27/2022 4:32 PM 2276 Microsoft Edge.lnk
-a--- 7/28/2022 12:47 PM 1363 PowerGREP 5.lnk
-a--- 7/1/2022 4:41 PM 1453 RegexBuddy 4.lnk
-a--- 6/17/2022 1:32 PM 877 TeamViewer.lnk
-a--- 6/30/2022 6:30 PM 993 Visual Studio Code.lnk
-a--- 9/14/2022 9:20 AM 1917 Zoom.lnk

Memory Usage of the c directory files in cmd

So, im running dir c:\ in command prompt and it shows me,
dir c:\
Volume in drive C is OS
Volume Serial Number is BE7A-83CF
Directory of c:\
12/06/2012 04:48 PM Accumedic
02/10/2016 10:27 AM 221,431 avgremover.log
12/23/2015 01:44 AM 3,072 Datacollectors.db
05/08/2012 05:45 PM DELL
02/18/2011 11:46 AM Drivers
02/02/2012 02:17 PM 23,760 FLBS1601.TEST
02/02/2012 02:29 PM 23,760 FLBS1602.TEST
02/18/2011 11:46 AM Install
02/06/2012 11:48 AM 22 Network.bat
07/13/2009 11:20 PM PerfLogs
02/01/2016 03:27 PM Program Files
10/14/2015 03:05 PM Program Files (x86)
12/27/2011 01:34 PM Projects
05/04/2016 08:52 AM SQL_Docs
04/07/2014 04:30 PM temp
07/23/2015 11:19 AM Users
05/04/2016 08:47 AM Windows
5 File(s) 272,045 bytes
12 Dir(s) 1,979,822,080 bytes free
Is there a command to see the disk usage of each of the files, instead of just the total? Preferably, in a similar format, and not individually looking each one up.
It is showing you the sizes of the files in the directory. From your question:
02/10/2016 10:27 AM 221,431 avgremover.log
12/23/2015 01:44 AM 3,072 Datacollectors.db
This is telling you that avgremover.log is 221,431 bytes in size. Most of the lines in your example don't have a number because they are directories. dir doesn't list a size for them.

Howto quickly gather file list with path and size information?

FindFirst/FindNext is slow. I see program like Defraggler can gather this list quickly. What Windows API they use ?
You are saying "FindFirst/FindNext" is slow. I think in terms of execution, _findfirst(), _findnext(), _findclose() #include <io.h> is as fast as it gets.
If you need very long path names, you need to use the Windows API version.
The Windows API is FindFirstFile(), FindNextFile(), and FindClose(), and the header file is #include <windows.h>.
The documentation for the Windows API tells you how to prepend "\?\" to the directory strings and get the maximum possible path length.
If you need more information than the C Library functions give you, then you need to use the Windows API FindFirstFileEx(), FindNextFileEx(), FindClose().
I have to refer you to the documentation for the details.
Simplest, C Library, _findfirst(): https://msdn.microsoft.com/en-us/library/zyzxfzac.aspx
More Info returned, Maximum path lengths, Windows API FindFirstFile(): https://msdn.microsoft.com/en-us/library/windows/desktop/aa364418(v=vs.85).aspx
Maximum Info returned, Maximum path lengths, Windows API FindFirstFileEx():
https://msdn.microsoft.com/en-us/library/windows/desktop/aa364419(v=vs.85).aspx
If the links are no longer valid, you probably can search something like "C Language _findfirst()", etc.
Not sure about Defraggler; however you could use powershell
If you are on a windows xp system you will have to download and install it
with powershell you can preform a simple query
for example:
$foo = Get-childItem -Path "c:\windows"
in powershell this will put all of the file names in the windows directory into an array called $foo
you can then go further and take the array and pipe it into a file
$foo >> c:\temp\test.txt
Example output:
Mode LastWrite Time Length Name
---- ------------- ------ ----
d---- 7/14/2009 1:32 AM Web
d---- 5/9/2012 3:20 AM winsxs
-a--- 2/3/2012 1:01 PM 16896 AsTaskSched.dll
-a--- 4/6/2011 12:46 AM 32200 atiogl.xml
-a--- 2/3/2012 12:35 PM 0 ativpsrm.bin
-a--- 11/20/2010 10:24 PM 71168 bfsvc.exe
-a--s 5/24/2012 10:17 PM 67584 bootstat.dat
-a--- 3/21/2012 11:58 PM 1908 diagerr.xml
-a--- 3/21/2012 11:58 PM 1908 diagwrn.xml
-a--- 5/4/2012 6:19 PM 28406 DirectX.log
For more info on powershell checkout
http://technet.microsoft.com/en-us/library/bb978526.aspx

Resources