Skip to main content

Useful Windows Powershell Script

Some of the useful powershell script which can be used in our day to day life.

1. Get running scheduled tasks on a Windows system.

(get-scheduledtask).where({$_.state -eq 'running'})

2. Get system uptime from multiple computers

Get-CimInstance Win32_operatingsystem -ComputerName $computers |
Select-Object PSComputername,LastBootUpTime,
@{Name="Uptime";Expression = {(Get-Date) - $_.LastBootUptime}}

3. Get drive utilization using PSDrives

Get-PSDrive -PSProvider filesystem | where-object {$_.used -gt 0} |
select-Object -property Root,@{name="SizeGB";expression={($_.used+$_.free)/1GB -as [int]}},
@{name="UsedGB";expression={($_.used/1GB) -as [int]}},
@{name="FreeGB";expression={($_.free/1GB) -as [int]}},
@{name="PctFree";expression={[math]::round(($_.free/($_.used+$_.free))*100,2)}}

4. List of Installed applications

Get-PSDrive -PSProvider filesystem | where-object {$_.used -gt 0} |
select-Object -property Root,@{name="SizeGB";expression={($_.used+$_.free)/1GB -as [int]}},
@{name="UsedGB";expression={($_.used/1GB) -as [int]}},
@{name="FreeGB";expression={($_.free/1GB) -as [int]}},
@{name="PctFree";expression={[math]::round(($_.free/($_.used+$_.free))*100,2)}}

5. Get details about all external scripts in your %PATH%.

gcm -commandtype externalscript | Get-Item |
Select-Object Directory,Name,Length,CreationTime,LastwriteTime,
@{name="Signature";Expression={(Get-AuthenticodeSignature $_.fullname).Status }}

6. Get event log utilization for remote computers defined in $computers

gcim Win32_NTEventLogFile -computer $computers -filter "NumberOfRecords > 0" |
Select-Object @{Name="Computername";Expression={$_.CSName}},
LogFileName,
NumberOfRecords,
@{Name="Size(KB)";Expression={$_.FileSize/1kb}},
@{Name="MaxSize(KB)";Expression={($_.MaxFileSize/1KB) -as [int]}},
@{name="PercentUsed";Expression={[math]::round(($_.filesize/$_.maxFileSize)*100,2)}} |
Sort Computername,PercentUsed |
Format-Table -GroupBy Computername -property LogFileName,NumberOfRecords,*Size*,PercentUsed

7. Get freespace for drive C on the local computer formatted in GB
(gcim win32_logicaldisk -filter "deviceid = 'C:'").FreeSpace/1gb

#or use the PSDrive

(gdr c).Free/1gb


8. Get a date string in the format year-month-day-hour-min-second


get-date -format yyyyMMddhhmmss


9. Get the last time your computer booted



(gcim win32_operatingsystem).LastBootUpTime

#or modify to get uptime

(get-date) - ((gcim win32_operatingsystem).LastBootUpTime)



10. Get configured TrustedHosts.


(get-wsmaninstance wsman/config/client).trustedhosts



11. Get all drives identified by a standard drive letter.


get-volume -driveletter (97..122) -ErrorAction SilentlyContinue



12. Get total physical memory formatted as GB


gcim win32_computersystem -computer SRV1,SRV2 | Select PSComputername,@{N="Memory";E={$_.TotalPhys
icalMemory/1GB -as [int]}}



13. Get IPv4 addresses on your local adapters.


Get-NetIPAddress -AddressFamily IPv4 | where-object IPAddress -notmatch "^(169)|(127)" | Sort-Object IPAddress | select IPaddress,Interface*



14. Find all processes that use a given module (dll).


get-process | Where { $_.Modules.filename -match "netapi32.dll"}



15. List all PowerShell profile script settings


$profile | select *host* | fl



16. Get the current date time as UTC time


(get-date).ToUniversalTime()
#or pretty it up
"$((get-date).ToUniversalTime()) UTC"

"$((get-date).ToUniversalTime().tolongdatestring()) UTC"



17. Get a formatted report of all commands with synopsis.


(Get-Command).where( { $_.source }) | Sort-Object Source, CommandType, Name |
Format-Table -GroupBy Source -Property CommandType, Name, @{Name = "Synopsis"; Expression = { (Get-Help $_.name).Synopsis}}



18. Unlock-ADAccount


Parameter Set: Default
Unlock-ADAccount [-Identity] <ADAccount> [-AuthType <ADAuthType> {Negotiate | Basic} ] [-Credential ,PSCredential]



Unlock-ADAccount -identity



19. Set-ADAccountPassword




$SecureString=ConvertTo-SecureString -string Abcd1234 -AsPlainText -Force

Set-ADAccountPassword -Identity TestAccount -Reset -NewPassword $SecureString

 

20. Remotely restart or shut down another computer

Start-Sleep 60; Restart-Computer –Force –ComputerName TARGETMACHINE

Stop-Computer –computer DC1 –Credential ashish\administrator

Comments

Popular posts from this blog

Comparison between Azure Application Gateway V1 and V2

Microsoft has announced new version of Azure Application Gateway and its Web Application Firewall module (WAF). In this article, we will discuss about the enhancements and new highlights that are available in the new SKUs i.e. Standard_v2 and WAF_v2. Enhancements and new features: Scalability: It allows you to perform scaling of the number of instances on the traffic. Static VIP: The VIP assigned to the Application Gateway can be static which will not change over its lifecycle. Header Rewrite: It allows you to add, remove or update HTTP request and response headers on application gateway. Zone redundancy: It enables application gateway to survive zonal failures which allows increasing the resilience of applications. Improved Performance: Improvement in performance during the provisioning and during the configuration update activities. Cost: V2 SKU may work out to be overall cheaper for you relative to V1 SKU. For more information, refer Microsoft prici

Difference between Azure Front Door Service and Traffic Manager

Azure Front Door Service is Microsoft’s highly available and scalable web application acceleration platform and global HTTP(s) load balancer. Azure Front Door Service supports Dynamic Site Acceleration (DSA), SSL offloading and end to end SSL, Web Application Firewall, cookie-based session affinity, URL path-based routing, free certificates and multiple domain management. In this article, I will compare Azure Front Door to Azure Traffic Manager in terms of performance and functionality. Similarity: Azure Front Door service can be compared to Azure Traffic Manager in a way that this also provides global HTTP load balancing to distribute traffic across different Azure regions, cloud providers or even with your on-premises. Both AFD & Traffic Manager support: Multi-geo redundancy: If one region goes down, traffic routes to the closest region without any intervention. Closest region routing: Traffic is automatically routed to the closest region. Differences: Azu

Install Solr as an Azure App Service

After Sitecore 9.0.2, Solr is a supported search technology for Sitecore Azure PAAS deployments. In this article, we will install SOLR service 8.4.0 in Azure App Service for Sitecore 10. 1. Create Azure App Service Login to Azure and create Azure App service. Make sure Runtime stack should be Java. 2. Download Solr Download Solr 8.4.0 from https://archive.apache.org/dist/lucene/solr/ Extract the files and add the below web.config file in the Solr package. <?xml version="1.0" encoding="UTF-8"?> <configuration>  <system.webServer>      <handlers>      <add  name="httpPlatformHandler"            path="*"            verb="*"            modules="httpPlatformHandler"            resourceType="Unspecified" />    </handlers>    <httpPlatform processPath="%HOME%\site\wwwroot\bin\solr.cmd"        arguments="start -p %HTTP_PLATFORM_PORT%"

Azure Machine Learning public preview announcement //Build, May 2021

Azure service updates Azure Machine Learning public preview announcement //Build, May 2021 New feature: Prebuilt Docker images for Inferencing, now in public preview. Click here for more information.

Azure Blob storage - Last access time tracking now generally available

Azure service updates Azure Blob storage - Last access time tracking now generally available This feature helps administrators control costs through the automatic tiering and deletion of blobs via tiering including when the data was last read. Click here for more information.