Skip to content

Instantly share code, notes, and snippets.

@bigbadmoshe
Last active July 18, 2024 08:02
Show Gist options
  • Save bigbadmoshe/81f60796d3557af699da1daf94ac7bca to your computer and use it in GitHub Desktop.
Save bigbadmoshe/81f60796d3557af699da1daf94ac7bca to your computer and use it in GitHub Desktop.
.Net
# .NET Types
$([System.Char])
$([System.Char]::ConvertFromUtf32())
$([System.Char]0x2013)
$([System.Char]0x00A9)
# Start a new process (e.g., Notepad)
$process = [System.Diagnostics.Process]::Start("notepad.exe")
# Wait for a few seconds to let the process start
Start-Sleep -Seconds 2
# Get information about the running process
$processInfo = [System.Diagnostics.Process]::GetProcessesByName("notepad")[0]
Write-Host "Process ID: $($processInfo.Id)"
Write-Host "Process Name: $($processInfo.ProcessName)"
Write-Host "Start Time: $($processInfo.StartTime)"
# Close the process
$processInfo.CloseMainWindow()
$processInfo.WaitForExit()
# Create a stopwatch instance
$stopwatch = [System.Diagnostics.Stopwatch]::StartNew()
# Code block to measure
Write-Host "Starting a task..."
Start-Sleep -Seconds 3
Write-Host "Task completed."
# Stop the stopwatch and display the elapsed time
$stopwatch.Stop()
Write-Host "Elapsed time: $($stopwatch.Elapsed.TotalSeconds) seconds"
# Define file path
$filePath = "$env:TEMP\example.txt"
# Create a new file and write some content
[System.IO.File]::WriteAllText($filePath, "This is a test file.")
# Read the content of the file
$fileContent = [System.IO.File]::ReadAllText($filePath)
Write-Host "File Content: $fileContent"
# Delete the file
[System.IO.File]::Delete($filePath)
Write-Host "File has been deleted."
# Define directory path
$directoryPath = "$env:TEMP\ExampleDirectory"
# Create a new directory
[System.IO.Directory]::CreateDirectory($directoryPath)
Write-Host "Directory created: $directoryPath"
# List all files and directories in the parent directory
$items = [System.IO.Directory]::GetFileSystemEntries($env:TEMP)
foreach ($item in $items)
{
Write-Host "Item: $item"
}
# Delete the directory
[System.IO.Directory]::Delete($directoryPath, $true) # $true to delete recursively
Write-Host "Directory has been deleted."
# Define the path for the log file
$logFilePath = "$env:TEMP\process_log.txt"
# Start a new process (e.g., Notepad)
$process = [System.Diagnostics.Process]::Start("notepad.exe")
# Create a stopwatch to measure the process runtime
$stopwatch = [System.Diagnostics.Stopwatch]::StartNew()
# Wait for the process to exit
$process.WaitForExit()
# Stop the stopwatch
$stopwatch.Stop()
# Log process information to a file
$logContent = @"
Process Name: $($process.ProcessName)
Process ID: $($process.Id)
Start Time: $($process.StartTime)
End Time: $(Get-Date)
Elapsed Time: $($stopwatch.Elapsed.TotalSeconds) seconds
"@
[System.IO.File]::AppendAllText($logFilePath, $logContent)
Write-Host "Process information has been logged to: $logFilePath"
[System.Diagnostics.Stopwatch]::StartNew()
[System.Diagnostics.Process]::Start()
[System.Diagnostics.Process]::GetProcessesByName()[0]
[Environment]::
[Environment]::MachineName
[Environment]::OSVersion
[Environment]::Is64BitOperatingSystem
[Environment]::Is64BitProcess
[Environment]::ProcessorCount
[Environment]::CurrentDirectory
[Environment]::SystemDirectory
[Environment]::UserDomainName
[System.Environment]::
[System.Management.ManagementClass]::GetInstances('Win32_Process')
[System.Management.ManagementClass]::GetInstances('Win32_Service')
[System.Management.ManagementClass]::GetInstances('Win32_ComputerSystem')
[System.Management.ManagementClass]::GetInstances('Win32_OperatingSystem')
[System.Management.ManagementClass]::GetInstances('Win32_NetworkAdapter')
[System.Management.ManagementClass]::GetInstances('Win32_LogicalDisk')
[System.Management.ManagementClass]::GetInstances('Win32_BIOS')
[System.Management.ManagementClass]::GetInstances('Win32_BaseBoard')
[System.Management.ManagementClass]::GetInstances('Win32_VideoController')
[System.Management.ManagementClass]::GetInstances('Win32_Processor')
[System.Net.NetworkInformation.NetworkInterface]::GetAllNetworkInterfaces()
[System.Management.ManagementObjectSearcher]::
## System.IO Namespace
[IOExceptionClass]
[System.IO.FileSystem.DriveInfo]::
[System.IO.FileSystemInfo]::
[System.IO.DirectoryInfo]::
[System.IO.Directory]::GetDirectories()
# https://learn.microsoft.com/en-us/dotnet/api/system.io?view=netcore-3.1
[System.IO.File]::
[System.IO.File]::Copy()
[System.IO.File]::Create()
[System.IO.File]::CreateText()
[System.IO.File]::Exists()
[System.IO.File]::Move()
[System.IO.FileInfo]::
[System.IO.Path]::
[System.IO.Path]::GetExtension()
[System.IO.FileStream]::
# Reads characters from a byte stream in a particular encoding.
[System.IO.StreamReader]::
# Writes characters to a stream in a particular encoding.
[System.IO.StreamWriter]::
# Provides common methods for sending data to and receiving data from a resource identified by a URI.
[System.Net.WebClient]::
# Represents an HTTP request to a Uniform Resource Identifier (URI).
[System.Net.HttpWebRequest]::
# Represents an HTTP response to a request.
[System.Net.HttpWebResponse]::
# Represents a regular expression pattern.
[System.Text.RegularExpressions.Regex]::
[Console]::
[Console]::BufferHeight
# PowerShell Cmdlets:
# Retrieves the child items (files and directories) in a specified location.
Get-ChildItem
# Reads the content of a file.
Get-Content
# Writes content to a file.
Set-Content
# Appends content to a file.
Add-Content
# Determines whether a path exists on the filesystem.
Test-Path
# Creates a new item (file or directory) at a specified location.
New-Item
# Deletes an item (file or directory) from the filesystem.
Remove-Item
# Starts one or more processes on the local computer.
Start-Process
# Sends HTTP and HTTPS requests to a web page.
Invoke-WebRequest
# Sends an HTTP or HTTPS request to a RESTful web service.
Invoke-RestMethod
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment