Powershell Commands

Windows powershell provides a powerful set of tools. You can use these tools to manipulate test data in a similar way to Linux.

SELECT STRING

Select-String provides a tool to search for a string in file. In this example we find the string “Error”.

Select-String -Path C:\*.log -Pattern "Error"

You can use select-string to parse the output from other commands. In this example we get all of the log files and search them for the string “Error”.

Get-ChildItem C:\ -Filter *.log -Recurse | Select-String "Error"

GET CHILDITEM

Gets the items and child items in one or more specified locations. We can use it to find items. In this example we look through all of the files in the folder for the term “avg”.

Get-ChildItem -Filter "*avg*" -Recurse -File

GET CONTENT

Gets the contents of a file/object. It can also be used to control the amount of data returned. In this example we get the contents of the jmeter.log file and display the last 7 lines.

Get-Content -Tail 7 .\jmeter.log

Or we can return the first 7 lines with the following command.

Get-Content -Head 7 .\jmeter.log

GET LOCATION

If you need to know the current directory you can use get-location.

Get-Location

NEW ITEM

If you want to create a new item/file new-item provides a method within powershell. In this example we create File1, File2, File3 and File4.

1..4 | ForEach-Object { New-Item -ItemType File -Name "File$_" }

EXPORT TO CSV

Powershell provides export-csv to enable you to save output in CSV format. In this example we get a list of process information and export the output to a CSV file.

Get-Process | Export-Csv -Path .\Processes.csv -NoTypeInformation

FURTHER READING

Powershell Cheat Sheet

Microsoft Powershell

Previous
Previous

Remote working

Next
Next

Performance Journey