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

Manjunath2019,
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 PowerShell.org <---- 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
4/5/2022
#>
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
switch($wmi_os.Version){
'10.0.10240'{$wmi_build="1507"}
'10.0.10586'{$wmi_build="1511"}
'10.0.14393'{$wmi_build="1607"}
'10.0.15063'{$wmi_build="1703"}
'10.0.16299'{$wmi_build="1709"}
'10.0.17134'{$wmi_build="1803"}
'10.0.17686'{$wmi_build="1809"}
'10.0.18362'{$wmi_build="1903"}
'10.0.18363'{$wmi_build="1909"}
'10.0.19041'{$wmi_build="2004"}
'10.0.19042'{$wmi_build="20H2"}
'10.0.19043'{$wmi_build="21H1"}
'10.0.19044'{$wmi_build="21H2"}
} <# This will need to be mmanually updated when microsoft changes/adds a build. Link here: https://docs.microsoft.com/en-us/windows/release-health/release-information
#>
$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
$Boot=[System.DateTime]::ParseExact($($wmi_os.LastBootUpTime).Split(".")[0],'yyyyMMddHHmmss',$null)
[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

Pause

<#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
Pause
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: