PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Forum experts available to answer your PowerShell questions

Power Tips

Power Tips

Syndication

  • Detecting 64-bit Operating System

    Windows 7/Server 2008 R2 To detect whether a script runs in a 32-bit or 64-bit environment is fairly easy: check a pointer size, and test whether it is 4 or 8 bytes: if ([ IntPtr ] :: Size -eq 8 ) { ' 64-bit ' } else { ' 32-bit ' } This...
  • Creating NTFS Security Report

    All PowerShell Versions If you'd like to audit NTFS permissions on your file servers, here is a suggestion how you could start. This script scans the Windows folder recursively for subfolders. Simply replace $Path with another path to scan other areas...
  • Finding Explicit Permissions

    All PowerShell versions Typically, NTFS permissions in the file system are inherited. You can, however, add explicit permissions to files and folders. To find out where inheritance was changed and direct security settings have been added, you can use...
  • 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...
« First ... < Previous 3 4 5 6 7 Next > ... Last »
Copyright 2012 PowerShell.com. All rights reserved.