Search This Blog

PowerShell - Functions

A function to find all files on the C: drive owned by a particular user:
 function Get-ByOwner
 {
   Get-ChildItem -recurse C:\ | get-acl | where {$_.Owner -match $args[0]} 
 }
PS C:\> Get-ByOwner JackFrost
A function to display PowerShell help on SS64.com, this will launch your default web browser:
 function Get-Help2
 {
   param([string]$command)
   Start-process -filepath "https://ss64.com/ps/$command.html"
 }
You can then do:
PS C:\> Get-Help2 -command set-acl

or because there is only one parameter:
PS C:\> Get-Help2 set-acl
A filter to display only files smaller than a given size:
 filter FileSizeBelow($size) { if ($_.length -le $size) { $_ } }
PS C:\> gci \\server64\workgroups -filter | FileSizeBelow 200kb
PS C:\> gci -recurse \\server64\workgroups | ?{!$_.PSIsContainer} | FileSizeBelow 100mb
A function with default values:
 function write-price
 {
  param([string]$description = "unknown",
        [int]$price = 100)
  Write-Output "$description ..... $price"
 }

PS C:\> write-price -price 250 -description Hammer
Hammer ..... 250
A filter to find files owned by a specific user:

 filter ownedbyme
 {
  if ($_.Owner -match "JackFrost") {$_}
 }

PS C:\> gci -recurse C:\ | Get-Acl | where {$_ | ownedbyme}

No comments:

Post a Comment