1. remove some text on file name of all files: e.g. “TVBOXNOW ”
1 |
ls | Rename-Item -NewName { $_.name -replace "TVBOXNOW ", "" } |
2. move up one level of files in each directory, e.g. c:\folder1\folder2\file3 to c:\folder1\file3
1 2 3 4 5 6 |
$files = Get-ChildItem -Recurse "c:\folder1" | where {$_.PSIScontainer -eq $false} foreach ($file in $files) { $destinationFolder = Split-Path -Parent $file.Directory.FullName move-item $file.FullName $destinationFolder } |
3. Remove files by extension for all subdirectories: e.g. .url / .nfo
1 |
Get-ChildItem -File -Include *.url,*.nfo -Recurse | foreach ($_) {Remove-Item $_.FullName} |
4. see unique file extension in all subdirectories
1 |
Get-Childitem -File -Recurse | Select-Object Extension -Unique |
5. file name format: xxxx.2013.zzzz, strip out .zzzz. “-WhatIf” for dry run
1 |
Get-ChildItem | ForEach-Object -Process { Rename-Item -Path $_.Name -NewName ($_.name -replace '(.*)(\d{4})(.*)$', '$1$2') -WhatIf } |
6. for all pdf or epub file, rename filename as folder name
1 2 |
Get-ChildItem -Recurse | Where-Object { $_.Extension -eq ".pdf" -or $_.Extension -eq ".epub" -and !$_.Name.StartsWith($_.Directory.Name) } | rename-item -newname {$_.Directory.Name + $_.Extension } -WhatIf |
For some business reasons, company provides FortiGate SSL VPN to connect office network, but some subnet cannot be accessed due to route did not add automatically. here is a PowerShell script to find the gateway and interface of VPN connection then add route. It will be great if I can find out how to run script automatically after VPN connection made. let say office VPN subnet is 10.50.10.0/24, add 2 new subnet by following
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
# add route required admin right, this will popup and ask for permission. If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { $arguments = "& '" + $myinvocation.mycommand.definition + "'" Start-Process powershell -Verb runAs -ArgumentList $arguments Break } # find VPN connection gateway IP and interface number $vpnroute = Get-WmiObject win32_IP4RouteTable | Where-Object { $_.Destination -eq "10.50.10.0" } $gateway = $vpnroute.NextHop $interface = $vpnroute.InterfaceIndex route add 10.50.23.0 mask 255.255.255.0 $gateway metric 20 if $interface route add 10.60.0.0 mask 255.255.0.0 $gateway metric 20 if $interface |
FortiGate SSL VPN client is based on PPP, different to Cisco or others as normal network adapter. ref: http://serverfault.com/questions/145259/powershell-win32-networkadapterconfiguration-not-seeing-ppp-adapter http://stackoverflow.com/questions/3293629/script-for-add-route photo credit: Brent Hensarling cc
Windows 7 default with PowerShell 2.0, when I downloaded “Windows Management Framework 4.0 “, installed and reboot then update failure, Error Code: 8004402F . it is due to temp directory on my system environment pointing to Ram Disk…. Turn off Ram disk, set TEMP back to somewhere with physical disk. M$, you win….. solution source here 4.0 comes with better support on networking. All I need is just interface Index after SSL VPN has been established, then add route. Haven’t finished that part though. Here is example to list connected adapters
1 2 |
get-wmiobject win32_networkadapter -filter "netconnectionstatus = 2" | select netconnectionid, name, InterfaceIndex, netconnectionstatus |