Using PowerShell to get a file name in a directory - powershell

I have a directory that contains a file called Bruce.txt. I need to copy the file name to a flat file. I tried using the copy-item command but that copies the contents not the name.
Is there a command to copy Bruce.txt (the name not the contents) to a flat file? So after it completes there will be a file called process.txt and its contents will be Bruce.txt. I tried using
Copy-Item -Path "C:\Users\Bruce\deploy\*.txt" -Destination "C:\Users\Bruce\process.txt".
Inside of deploy is a text file called Bruce.txt with the contents of select count() from EMP_NR.
I need Bruce.txt the name copied to process.txt not the Select count() etc.
For Shell script I use the ls command and it works wonderful what can I use for Powershell?

You need to use the Get-ChildItem cmdlet.
Get-ChildItem C:\Users\Bruce\deploy\*.txt | Select-Object -ExpandProperty Name | Out-File C:\Users\Bruce\process.txt -Force -Append
However, as you're using PowerShell, ls would actually work for you here, as would gci and dir as they're all aliases for Get-ChildItem:
> get-alias | ? {$_.DisplayName -ilike "*get-childitem*" }
CommandType Name
----------- ----
Alias dir -> Get-ChildItem
Alias gci -> Get-ChildItem
Alias ls -> Get-ChildItem
You can also use > or >> instead of piping to Out-File if you so wish.
Because the Get-Childitem cmdlet returns a list of objects, you then need to also select which information you want to extract from the object. If you do a ls in a directory with some content, you will see the contents are formatted into a table.
By using the Select-Object cmdlet, you can extract the object properties you want to write to your file, in this case the Name property.

Related

Powershell script to list files without folder names

I open a PS in a folder then use
dir -name > asd.xls -recurse.
How can I modify this so it doesn't incude folders in the filenames?
Instead of using -name, try using
(Get-ChildItem -Recurse).Name > asd.xls
and be aware that you won’t get a valid Excel workbook that way. You can get a valid CSV that can be loaded into Excel with
(Get-ChildItem -Recurse) | Select-Object -Property Name | Export-CSV -Path asd.csv -NoTypeInformation

Exclude extension from an output file in PS?

My goal is to display items in the directory C:\test in a log file called log.txt without displaying the file-extensions of the files found, e.g. .zip, .pdf, etc.
My script so far:
Get-ChildItem -Path C:\Test\ -name |Out-File C:\test2\log.txt
How do I get the .log file to NOT display the extensions of the files found in the C:\test folder?
Use BaseName property instead of Name:
Get-ChildItem -Path C:\Test\ | Select-Object -ExpandProperty BaseName | Out-File C:\test2\log.txt
As there's no built-in -BaseName property for Get-ChildItem cmdlet, you need to get that property using Select-Object. Expanding the property allows you to get only the value of chosen property, without the header.
Another way to get BaseName value would be to use .BaseName like this:
(Get-ChildItem -Path C:\Test\).BaseName | Out-File C:\test2\log.txt
That form is shorter, but personally I prefer the first one due to readability and no need to remember about surrounding braces ().
Best practice
If you want to inspect what are the possible properties (and their values) of the object you have, you can also use Select-Object for that:
# Warning: HUGE OUTPUT POSSIBLE
Get-ChildItem -Path C:\test\| Select-Object *
# It's usually good to take only one object from the array
$obj = (Get-ChildItem -Path C:\test\)[0]
$obj | Select-Object *

Filter files with a extesion and run a command for all found files in Windows PowerShell

I want to find all the files having .zip extension in a folder (MyFiles) recursively and run the following command for each file in Windows PowerShell.
PS C:\solr-7.3.0> java -Dc=myCore1 -Dauto=yes -Ddata=files
-Drecursive=yes -jar example/exampledocs/post.jar "File fullpath goes here"
Could you help me to achieve this?
Use the Get-ChildItem cmdlet to find the relevant Zip files and then pipe the results to the ForEach-Object cmdlet to loop over the files. The $_ or $psitem variable is the current object passed through the pipeline. Then the FullName property on that object will contain the full path to each Zip file.
Get-ChildItem -Path C:\Example\Path -Filter '*.zip' -Recurse |
ForEach-Object {
& java -Dc=myCore1 -Dauto=yes -Ddata=files -Drecursive=yes -jar example/exampledocs/post.jar $_.Fullname
}

Get Directories Names Only

I have a directoy X that has say 500 subdirectories. What I need is a quick way to just get only my directory and the names of these 500 subdirectories in my X directory (so, no Mode, no LastWriteTime or anything else, just the name) and pipe it to a file.
So, for example, I have this:
-X
|+Dir1
|+Dir2
|+Dir3
|
...
|+Dir500
What I want to get piped to a txt file is this
X/Dir1
X/Dir2
X/Dir3
...
X/Dir500
How can I do this using PowerShell or CommandLine?
I am using Windows 7 and PowerShell 4.0
Thanks,
Get-ChildItem will do the same thing as dir in command-line: it gets whatever is in your directory. You're looking only for directories. PS v3 and up has this built-in by using the flag of -directory. In older PowerShell versions, you can pipe your Get-ChildItem to a Where{$_.PSIsContainer to get directories and then pipe that to select Name to just get the names of the directories, not their full paths (e.g. "Dir1" vs. "X:\Dir1"). If you want the full path, use select FullName. Assuming you want that as a CSV, pipe that to Export-Csv followed by the name of the CSV you're creating, such as DirectoriesInX.csv and if you don't want the type information, add the flag of -NoTypeInformation.
PowerShell v3 and up:
Get-ChildItem "X:\" -directory | Select FullName | Export-Csv "DirectoriesInX.csv" -NoTypeInformation
PowerShell v2 and below:
Get-ChildItem "X:\" | Where{$_.PSIsContainer} | Select FullName | Export-Csv "DirectoriesInX.csv" -NoTypeInformation
I would have not used -Recurse based on requirement.
Moreover, OP wants to pipe output to a file :
(Get-ChildItem "X" -Directory).FullName | Out-File c:\myList.txt
The -Directory switch is only available from PS3.
The -Recurse switch would go as deep as possible in the tree and list all folders

How can I list and print files in a directory?

I'm trying this but it doesn't print anything:
Dir -Recurse "C:\temp" | Select Fullname
Looks like this command just selects file names. I want to see them in console.
Take a look at Get-Childitem
Dir -Recurse c:\path\ | Get-Childitem
Concerning your code in the question.
Your command should have worked as is. You are, in fact, already calling Get-ChildItem. If you check Get-Alias you will see what I'm trying to tell you.
PS C:\users\Cameron\Downloads> Get-Alias dir
CommandType Name ModuleName
----------- ---- ----------
Alias dir -> Get-ChildItem
You code translates to
Get-ChildItem -Recurse "C:\temp" | Select Fullname
Again, I'm not sure why your code does not generate output since that is perfectly fine on a folder that contains files or directories. Might be an issue with the positional parameter maybe? What is your PowerShell version? ( Use Get-Host).
The code you have would send all file paths to console. Did you want that output somewhere else?
About the accepted answer
Pretty sure this code will double up output if you have folders in the path since directory will output to the second Get-ChildItem
Dir -Recurse c:\path\ | Get-Childitem
Consider the following folder tree
C:\TEMP\TEST
│ File1.txt
│ File2.txt
│
└───Folder1
File3.txt
Consider the two command run against that folder tree.
PS C:\users\Cameron\Downloads> Dir -Recurse c:\temp\test | Select Fullname
FullName
--------
C:\temp\test\Folder1
C:\temp\test\File1.txt
C:\temp\test\File2.txt
C:\temp\test\Folder1\File3.txt
PS C:\users\Cameron\Downloads> Dir -Recurse c:\temp\test | Get-Childitem | Select Fullname
FullName
--------
C:\temp\test\Folder1\File3.txt
C:\temp\test\File1.txt
C:\temp\test\File2.txt
C:\temp\test\Folder1\File3.txt
The second command shows two files called File3.txt when in reality there is only one.
get-childitem | format-list > filename.txt
This will give you a text file with name, size, last modified, etc.
if you want specific parameters from the item... such as name of the file only the command is
get-childitem | format-list name > filename.txt
this is give you the same text file, but with just the name of the files listed.
It might also be worth mentioning the -force switch which is required to see hidden items.