Monthly Archives: September 2012

how to use sort/where object

to sort the output use pipe “|”

Get-ChildItem | Sort-Object

Directory: C:\myScripts

Mode LastWriteTime Length Name
—- ————- —— —-
-a— 8/25/2012 2:35 PM 298 alias.txt
-a— 9/13/2012 8:18 PM 242 template.ps1
-a— 9/13/2012 10:37 PM 7610 ThreeAmigos.ps1

To override default property used by Sort-Object you need to specify the property:
Get-ChildItem | Sort-Object -Property length

Directory: C:\myScripts

Mode LastWriteTime Length Name
—- ————- —— —-
-a— 9/13/2012 8:18 PM 242 template.ps1
-a— 8/25/2012 2:35 PM 298 alias.txt
-a— 9/13/2012 10:37 PM 7610 ThreeAmigos.ps1

To narrow the query even further the Where-Object can be used:

Get-ChildItem | Sort | Where-Object { $_.Length -gt 242 }

Directory: C:\myScripts

Mode LastWriteTime Length Name
—- ————- —— —-
-a— 8/25/2012 2:35 PM 298 alias.txt
-a— 9/13/2012 10:37 PM 7610 ThreeAmigos.ps1

General info

cmdlets are not case sensitive.

$_ is a special variable created and maintained by Windows PowerShell. $_ is automatically assigned the
of the current object in the PowerShell pipeline and, in the case of the Where-Object cmdlet, to reference each object
in a collection. The collection can be composed of every file in the current working directory see example.
—————————————————————————————————————————————————————-
Get-Member
Gets the properties and methods of objects.

e.x.
Get-ChildItem template.ps1 | Get-Member -Name C*

TypeName: System.IO.FileInfo

Name MemberType Definition
—- ———- ———-
CopyTo Method System.IO.FileInfo CopyTo(string destFileName), System.IO.FileInfo CopyTo(string destFileName, bool overwrite)
Create Method System.IO.FileStream Create()
CreateObjRef Method System.Runtime.Remoting.ObjRef CreateObjRef(type requestedType)
CreateText Method System.IO.StreamWriter CreateText()
CreationTime Property System.DateTime CreationTime {get;set;}
CreationTimeUtc Property System.DateTime CreationTimeUtc {get;set;}

—————————————————————————————————————————————————————-

to display methods within an object

to retrieve a listing of all the methods associated with a given object you need to specify:
Get-ChildItem template.ps1 | Get-Member -MemberType Method

TypeName: System.IO.FileInfo

Name MemberType Definition
—- ———- ———-
AppendText Method System.IO.StreamWriter AppendText()
CopyTo Method System.IO.FileInfo CopyTo(string destFileName), System.IO.FileInfo CopyTo(string destFileName, bool…
Create Method System.IO.FileStream Create()
CreateObjRef Method System.Runtime.Remoting.ObjRef CreateObjRef(type requestedType)
CreateText Method System.IO.StreamWriter CreateText()
Decrypt Method System.Void Decrypt()
Delete Method System.Void Delete()
Encrypt Method System.Void Encrypt()
Equals Method bool Equals(System.Object obj)
GetAccessControl Method System.Security.AccessControl.FileSecurity GetAccessControl(), System.Security.AccessControl.FileSe…
GetHashCode Method int GetHashCode()
GetLifetimeService Method System.Object GetLifetimeService()
GetObjectData Method System.Void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Seria…
GetType Method type GetType()
InitializeLifetimeService Method System.Object InitializeLifetimeService()
MoveTo Method System.Void MoveTo(string destFileName)
Open Method System.IO.FileStream Open(System.IO.FileMode mode), System.IO.FileStream Open(System.IO.FileMode mo…
OpenRead Method System.IO.FileStream OpenRead()
OpenText Method System.IO.StreamReader OpenText()
OpenWrite Method System.IO.FileStream OpenWrite()
Refresh Method System.Void Refresh()
Replace Method System.IO.FileInfo Replace(string destinationFileName, string destinationBackupFileName), System.IO…
SetAccessControl Method System.Void SetAccessControl(System.Security.AccessControl.FileSecurity fileSecurity)
ToString Method string ToString()

how to display additional info about files

to display standard information about a file
Get-ChildItem template.ps1

Directory: C:\myScripts

Mode LastWriteTime Length Name
—- ————- —— —-
-a— 9/13/2012 8:18 PM 242 template.ps1

To find out what properties are stored by .NET Framework
Get-ChildItem template.ps1 | Get-Member -MemberType Property

TypeName: System.IO.FileInfo

Name MemberType Definition
—- ———- ———-
Attributes Property System.IO.FileAttributes Attributes {get;set;}
CreationTime Property System.DateTime CreationTime {get;set;}
CreationTimeUtc Property System.DateTime CreationTimeUtc {get;set;}
Directory Property System.IO.DirectoryInfo Directory {get;}
DirectoryName Property System.String DirectoryName {get;}
Exists Property System.Boolean Exists {get;}
Extension Property System.String Extension {get;}
FullName Property System.String FullName {get;}
IsReadOnly Property System.Boolean IsReadOnly {get;set;}
LastAccessTime Property System.DateTime LastAccessTime {get;set;}
LastAccessTimeUtc Property System.DateTime LastAccessTimeUtc {get;set;}
LastWriteTime Property System.DateTime LastWriteTime {get;set;}
LastWriteTimeUtc Property System.DateTime LastWriteTimeUtc {get;set;}
Length Property System.Int64 Length {get;}
Name Property System.String Name {get;}

To display customized information:
Get-ChildItem template.ps1 | Select-Object name,extension,directory, exists

Name Extension Directory Exists
—- ——— ——— ——
template.ps1 .ps1 C:\myScripts True