Power Tips

Share |

Join PowerShell.com!
Subscribe to Feed

PowerShell eBook
Sign up for
Your PowerTip of the Day:

Powershell Plus


13 Free SQL Tools from Idera


Free Ebook: Mastering Powershell

Welcome to the archive of tips delivered through Tobias' Tip of the Day RSS Feed and Your Power Tip of the Day email. Subscribe in the sidebar to get the latest tips!

Sort by: Most Recent | Most Viewed | Most Commented
  • Accessing COM Objects without ProgID

    All Versions Typically, to access COM objects, these objects need to register themselves in the Windows Registry, and PowerShell needs the registered ProgID string to load the object. Here is an example: $object = New-Object -ComObject Scripting.FileSystemObject...
  • Dealing with Hidden Files

    PowerShell 3.0 and later When you use Get-ChildItem to list files, by default, hidden files are excluded. To include hidden files, use the –Force parameter: PS> Get-ChildItem -Path $home -Force To only list hidden files, use the -Hidden parameter...
  • Changing GPO Description/Comment

    GroupPolicy Module When you create a new Group Policy, you can set a comment (or description). There is no apparent way, however, to change the description later. Here is code that allows you to retrieve a group policy, then read and/or change the description...
  • Checking PowerShell Security

    PowerShell 2.0 and later This sample code finds all PowerShell scripts in a given drive, then checks the scripts for valid digital signatures, and reports those scripts that either have no signature, or have an invalid signature: Get-ChildItem C:\ -Filter...
  • Dealing with %ERRORLEVEL%

    All PowerShell versions When you run native EXE console commands inside your scripts, these commands typically return a numeric return value. This value is known as "ErrorLevel", and in batch files, you would refer to the return value as %ERRORLEVEL...
  • Compacting Paths

    PowerShell 2.0 and later Sometimes path names can get too large for reports. To shorten a path, you could of course clip the string to a given length, but this would make the path meaningless. A better way might be to use the internal Windows API function...
  • Get the OU from an LDAP Path

    All Versions To extract certain parts from raw strings, you can often use a combination of text splitting and text substring commands. For example, to extract the name of the last OU in an LDAP path, here is an approach: $dn = ' OU=Test,OU=People...
  • Creating Huge Dummy Files

    All PowerShell versions If you need to stress test systems, or need large dummy files for other purposes, here is some code that can create even very large files in a split second: $Path = " $env:temp\hugefile.txt " $Size = 200 MB $stream =...
  • Limiting Maximum String Length

    All Versions To make sure output text is not excessively long, you can use logic like this to shorten text that is longer than a given maximum length: if ( $text . Length -gt $MaxLength ) { $text . Substring ( 0 , $MaxLength ) + ' ... ' } else...
  • Setting Permissions in AD or Windows Registry

    ActiveDirectory Module We already illustrated previously how you can use Get/Set-Acl to read and write permissions to files and folders. The truth is that both cmdlets can deal with any valid PowerShell path. So you can use them in the exact same way...
  • Catching Errors in Native EXEs (Part 2)

    All versions Here is yet another approach that you can use to detect errors emitted by console applications: $ErrorActionPreference = ' Continue ' $result = net.exe user UserDoesNotExist 2 >& 1 # $? is $false when something went wrong if...
  • Replacing NTFS Permissions with SDDL Information

    All PowerShell versions With Get-Acl, you can output the security information from files and folders as plain text in SDDL format (Security Descriptor Definition Language): $FolderToRead = ' C:\folder1 ' $securityDescriptor = Get-Acl -Path $FolderToRead...
  • Cloning NTFS Permissions

    All PowerShell Versions Here is some simple code that reads the NTFS permissions from one folder and applies the exact same settings to another. Note that both folders must exist: $FolderToCopyFrom = ' C:\folder1 ' $FolderToCopyTo = ' C:\folder2...
  • Catching Errors in Native EXEs (Part 1)

    All Versions When you run native console EXE commands such as robocopy.exe, ipconfig.exe, or similar commands, you can handle errors raised by these commands: try { $current = $ErrorActionPreference $ErrorActionPreference = ' Stop ' # this will...
  • Using WMI Inheritance

    All Versions WMI classes are inherited from each other, and that’s something you can take advantage of. Take this line: PS> Get-WmiObject -Class Win32_Printer It will return all printers found by WMI. Printers are inherited from more generic...
« First ... < Previous 2 3 4 5 6 Next > ... Last »
Copyright 2012 PowerShell.com. All rights reserved.