Search This Blog

Friday, November 22, 2013

Great post from the Hey, Scripting Guy! Blog

Ran across this script on the  Hey, Scripting Guy! Blog

Use PowerShell to Remove Data from CSV File by Matt Tisdale


If you create a number of reports that have data not required for all end users this is a fast way to remove the columns that are not applicable to each user,  I know that it may be just as easy to create new reports for each users need, but if you are tasked with creating a report it may provide the data the user needs immediately as you work to capture the data the user may need 

Adding date stamp to filename using Powershell

The previous post was about adding the date stamp to files that were already created. This script would prevent you from having to do it again. If having the datestamp is necessary in the filename, why not put it in when you create the file.

The script below will save the date as you create the file

Get-Process | Export-Csv test_$((Get-Date).ToString('ddMMMyy')).csv

In this example test is the name of the file and $((Get-Date).ToString('ddMMMyy')) puts the the date after in the format of  Day Month Year

Output would look like test_22Nov13.csv

Note: You need to change the name "test" to something meaningful to the file being saved  or you could pass the variable to be saved.

$Filename = read-host "what would you like to call the file"
$Computername =  read-host "Which Computer"
Get-Process -ComputerName $Computername  | Export-Csv ($Filename.ToUpper() +"_"+ $((Get-Date).ToString('ddMMMyy')) +".csv")

Powershell to rename a group of files by adding the Created Date to the name.

If you have a need to add the date to a file name to help identify when it was created this script may have a use to to you.

It will search through a folder and rename a file by appending the date created to the original name of the file.

Get-ChildItem -Filter "*.csv" -Recurse | Rename-Item -NewName {$_.BaseName + $_.CreationTime.toString("ddMMMyy") + ".csv" }

Note:
The above script will only look for files in the path that you are currently in in powershell. 

Example of the output

original filename was something.csv

new filename  becomes something22Nov13.csv