Code to Open File, Select All and Copy - powershell

I am trying to create a script that will open up a .txt file and select and then copy the data in the .txt file. I am able to open the .txt file but what I am not able to do is create a script to open, select and copy at the same time and in the same script line.
Here are some code examples that I have tried:
Invoke-Item adddata.txt; object.SendKeys "^(a)"
Invoke-Item adddata.txt; WshShell.SendKeys "^"; WshShell.SendKeys "a"
Invoke-Item adddata.txt; WshShell.SendKeys "{^}a"
What happens is the file does open up but nothing selects/copies on either example.

I think the Get-Content cmdlet is what you should be using:
Get-Content -Path "C:\ExampleFolder\adddata.txt" | clip

I think php123's answer does the job, however because your paste target is Excel and if the data in your file is in columns with a delimiter and you can improve the final result. You need to use the Import-Csv cmdlet to convert to an object and then use the ConvertTo-Csv to turn you object into a tab delimited string which will then paste nicely into Excel. Something like this assuming your file is comma separated:
Import-Csv "C:\ExampleFolder\adddata.txt" -Delimiter ',' | ConvertTo-Csv -Delimiter "`t" | clip

Related

Close open CSV before writing data back to it. Powershell

I have a csv file like so:
$csv = Import-Csv "C:\data.csv"
Followed by code that formats the csv data.
When I write to the file
$csv | Export-Csv -path "C:\data.csv" -NoType
If the file is open I get (predictably)
**Export-Csv : The process cannot access the file 'C:\data.csv' because it is being used by another process.**
There are times the file may be saved yet remain open when interacting with the script. I'd like to efficiently check for the open file and close it so I can write the data back to the same file/variable.
Ideally there is nothing like closing file in Powershell as it handles it automatically. Below code worked for me
$csv = Import-Csv "C:\data.csv"
$csv | Export-Csv -path "C:\data.csv" -NoType

Filtering data from CSV file with PowerShell

I have huge csv file where first line contains headers of the data. Because the file size I can't open it with excel or similar. I need to filter rows what I only need. I would want to create new csv file which contains only data where Header3 = "TextHere". Everything else is filtered away.
I have tried in PowerShell Get-Content Select-String | Out-File 'newfile.csv' but it lost header row and also messed up with the data putting data in to wrong fields. There is included empty fields in the data and I believe that is messing it. When I tried Get-Content -First or -Last data seemed to be in order.
I have no experience handling big data files or powershell before. Also other options besides PowerShell is also possible if it is free to use as "non-commercial use"
try like this (modify your delimiter if necessary):
import-csv "c:\temp\yourfile.csv" -delimiter ";" | where Header3 -eq "TextHere" | export-csv "c:\temp\result.csv" -delimiter ";" -notype

Append the changes in list of files to csv

Problem Statement: -
I want to extract the filenames with their timestamp to a csv or excel file from an UNC path in windows. The content of folder location may change and hence I want to capture the day when the filename was added to the output file. The output file should only append the changes that happened from last time. For example, if more files got added to the location those should be captured but if some got deleted they should be as it is in the output file.
Expected Output: -
Code Created so far: -
Get-ChildItem -Path \\UNC_Path\DEV\REQUEST\SCD\ARCHIVE\*.txt |
Select-Object -Property Name,LastWriteTime |
Export-Csv -Append -Path \\UNC_Path\DEV\REQUEST\SCD\ARCHIVE\Output.csv -Encoding ascii -NoTypeInformation
This gives an output like below: -
Can someone guide me to append only the changes and add date column when particular row was captured. I am a newbie to both powershell and stackoverflow. Any suggestions would be appreciated.

Append filename to CSV in new column

I have a lot of csv files stored in a directory.
In each csv file need to add the name as column this through powershell.
Example
File location: <SERVERNAME>\Export\FILENAME1.CSV
Contents:
1232;Description;a1
1232;Description;a2
The result must be:
1232;Description;a1;FILENAME1.CSV
1232;Description;a2;FILENAME1.CSV
Can someone help me with this?
The following will append a Filename column to each .CSV file in a directory:
Get-ChildItem *.csv | ForEach-Object {
$CSV = Import-CSV -Path $_.FullName -Delimiter ";"
$FileName = $_.Name
$CSV | Select-Object *,#{N='Filename';E={$FileName}} | Export-CSV $_.FullName -NTI -Delimiter ";"
}
Explanation:
Uses Get-ChildItem to get all files named *.csv
Iterates through each file with ForEach-Object and uses Import-CSV to load their contents as a PowerShell object
Records the name of the file in $FileName
Uses Select-Object to add a calculated property with the name Filename and the value of the $FileName variable
Uses Export-CSV to write back over the original file. The -NTI (NoTypeInformation) switch is used to ensure the PowerShell object header line is not included.
Mark Wragg's PowerShell code works, but I had to change the delimiter to , instead of ; so that it opens in Excel. I'm trying to figure out how to append the filename to the first column instead of the last because my files don't have the same number of fields so they don't align.

PowerShell - piped property not not working as I had hoped

I am a little new to PowerShell, so this is probably a basic question.
I have written a small one-liner to remove the first 97 lines from the top of each text file in a directory.
The script works in as far as removing the line, but the new file created at the end doesn't have the name I expected. Here is the script:
Get-ChildItem | ForEach-Object {Get-Content $_.PSPath | Select -Skip 97 | Set-Content "Edited-$_.PSChildName" }
The original file is called:
file.txt
What I expect the new file to be called is:
Edited-file.txt
The file actually comes out as:
Edited-file.txt.PSChildName
Any idea what I am doing wrong?
I think you want Set-Content "Edited-$($_.PSChildName)". The $() allows you to interpolate expressions into strings e.g. "abc$(2+2)" returns the string "abc4".