Create a csv file with local PC information

hi, can you please solve this prob

Powershell scripting

Create a csv file with local PC information
Send the above generated file over email

Welcome once again to the forum. :wave:t4: :wink:

This is a peer to peer forum where we help each other with self written code. We do not deliver complete scripts or solutions on request, sorry.

What have you tried so far? We expect you to make an own attempt to get your task done or to solve your problem. If you have done so already please document here what exactly you have done and show your code. Then we probably might be able to help you a step further.

Hi Olaf,

can you please tell me how can we send this file through mail as csv file using powershell script

$PCinfo = @() #array

#HashTable declaration
$Properties = @{
“Device name” = “DESKTOP-682GE80”;
“Processor” = “Intel(R) Core™ i3”;
“Installed RAM” = “4.00 GB”
“Product ID” = “00327-30254-49675-AAOEM”
“System type” = “64-bit operating system”


$PCinfo = New-Object -TypeName PSObject -prop $Properties
$MyInfo = $PCinfo
$MyInfo | Export-Csv -Path .\test
$raw_data = Import-Csv -Path .\test

When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in <---- Click :point_up_2:t4: :wink:

The cmdlet Send-MailMessage has a parameter -Attachments for this purpose. Please read the help completely including the examples to learn how to use it.

I am working on the same thing now.
I will share what I have so far. It gathers a lot of information we use.
If anyone would like to gently nudge me on the last part I would appreciate it.
I generally frankenstein my scripts, so i realize that it can probably be done a lot better!

Thanks in advance!!

Script will get several bits of information on a devcie as log as you can ping it
Write-Host "This will get all information needed from a device(as long as you can ping it)!" -ForegroundColor Red -BackgroundColor Yellow
$device = Read-Host "Type the device ID here"
$builddate = gcim Win32_operatingSystem | select InstallDate
$Manufacturer = Get-WmiObject -ComputerName $Device -class win32_computersystem | select -ExpandProperty Manufacturer
$Model = Get-WmiObject -class win32_computersystem -ComputerName $Device | select -ExpandProperty model
$Serial = Get-WmiObject -class win32_bios -ComputerName $Device | select -ExpandProperty SerialNumber
$wmi_os = Get-WmiObject -class Win32_OperatingSystem -ComputerName $Device | select CSName,Caption,Version,OSArchitecture,LastBootUptime
} <# This will need to be mmanually updated when microsoft changes/adds a build. Link here:
$wmi_cpu = Get-WmiObject -class Win32_Processor -ComputerName $Device | select -ExpandProperty DataWidth
$wmi_memory = Get-WmiObject -class cim_physicalmemory -ComputerName $Device | select Capacity | %{($_.Capacity / 1024kb)}
$DNName = Get-ADComputer -Filter "Name -like '$Device'" | select -ExpandProperty DistinguishedName
[TimeSpan]$uptime = New-TimeSpan $Boot $(get-date)
$Userinfo = Get-WmiObject –ComputerName $device –Class Win32_ComputerSystem | Select-Object UserName
$MAC = Get-WmiObject win32_networkadapterconfiguration -computername $device -Filter 'IpEnabled=True' |  select macaddress
$Diskinfo = Get-WmiObject Win32_LogicalDisk -ComputerName $device -Filter DriveType=3 | Select-Object DeviceID, @{'Name'='Size (GB)'; 'Expression'={[string]::Format('{0:N0}',[math]::truncate($_.size / 1GB))}}, @{'Name'='Freespace (GB)'; 'Expression'={[string]::Format('{0:N0}',[math]::truncate($_.freespace / 1GB))}}

Write-Host "------Computer Info for $Device------------------" -ForegroundColor RED -BackgroundColor Yellow 
Write-host " "
Write-Host "Hostname/Serial Number:" -ForegroundColor Red -BackgroundColor Yellow 
Write-host "$($wmi_os.CSName) / $serial"
Write-host " "
Write-Host "Build Date:" -ForegroundColor Red -BackgroundColor Yellow 
Write-Host "$builddate"
Write-host " "
Write-Host "Computer OU Information:"-ForegroundColor Red -BackgroundColor Yellow 
Write-host "$DNName"
Write-host " "
Write-Host "Device Make Model" -ForegroundColor Red -BackgroundColor Yellow 
Write-Host "$Manufacturer $Model  $($wmi_os.Caption) $wmi_build $($wmi_os.OSArchitecture) $($wmi_os.Version)"
Write-host " "
Write-Host "Memory" -ForegroundColor Red -BackgroundColor Yellow 
Write-Host "$wmi_memory"
Write-host " "
Write-Host "Uptime:" -ForegroundColor Red -BackgroundColor Yellow
Write-Host "$($uptime.days) Days $($uptime.hours) Hours $($uptime.minutes) Minutes $($uptime.seconds) Seconds"
Write-host " "
Write-Host "Last user Logged in:" -ForegroundColor Red -BackgroundColor Yellow 
Write-Host "$userinfo"
Write-host " "
Write-Host "Harddrive Info:" -ForegroundColor Red -BackgroundColor Yellow 
Write-Host "$diskinfo"
Write-host " "
Write-host "MAC Address:" -ForegroundColor Red -BackgroundColor Yellow 
Write-Host "$MAC"
Write-host " "
Write-Host "IPV4/IPV6 address" -ForegroundColor Red -BackgroundColor Yellow 
Get-WmiObject Win32_NetworkAdapterConfiguration | where { (($_.IPEnabled -ne $null) -and ($_.DefaultIPGateway -ne $null)) } | select IPAddress -First 1

Write-Host "Last Devcie Checkin Date to AD and IP" -ForegroundColor Red -BackgroundColor Yellow 
Get-ADComputer "$device" -Properties * | FT LastLogonDate, IPv4Address


<#I am tryig to get this to write to a file
Creates Dir in C drive
mkdir "C:\PSEXPORTS" -force | Out-Null
$tempname = Get-Random -Maximum 100
Write-Host "This will export all the information it to a csv here C:\PSEXPORTS\devcie name" -ForegroundColor Red -BackgroundColor Yellow
Export-csv -path "C:\PSEXPORTS\$tempname.csv"
Get-ChildItem c:\psexports\$tempname.csv | Rename-Item -NewName ("$device-"  + "$Time" +  + ".csv")

Please, even if it fits perfectly - do not hijack other peoples threads. Instead create an own new one and link the thread you’re refering to in yours.

Thanks in advance.

I did not feel sharing what I had with someone trying to get the results was hijacking.
I was only attempting to assist with a request as well as learn.

Hmmm … so I’ve mistaken you, sorry for that. :pray:t4: But the actual question from @manjunath2019 was how to send an email with the collected information!? :wink:

Correct if I’m wrong - that sounds like you’re asking for help, aren’t you? And you actually didn’t even say what your last part is, what you need help with. :smirk: